DEV Community

Guna Sekaran
Guna Sekaran

Posted on

LCM AND GCD PROGRAM IN JAVA.

      FINDING LCM:


package ifelse;

public class Lcm {
    public static void main(String[] args) {
        int no1=2;
        int no2=4;
        int big=0
        if(no1>no2) 
            big=no1;
        else
            big=no2;
    while(true) {

        if (big%no1==0 && big%no2==0);
        System.out.println("LCM IS:"+big);
        break;


    }
    big++;  

    }

}

   OUTPUT:
       LCM IS:4
Enter fullscreen mode Exit fullscreen mode
package ifelse;

public class Lcm {
    public static void main(String[] args) {
        int no1=2;
        int no2=4;
        int big=0;
        int limit=0;
        if (no1 > no2) {
            big = no1;
        } else {
            big = no2;
        }

       while(big<100) {

        if (big%no1==0 && big%no2==0)
        System.out.println("LCM IS:"+big);


        big++;      
    }


}

}



OUTPUT:
LCM IS:4
LCM IS:8
LCM IS:12
LCM IS:16
LCM IS:20
LCM IS:24
LCM IS:28
LCM IS:32
LCM IS:36
LCM IS:40
LCM IS:44
LCM IS:48
LCM IS:52
LCM IS:56
LCM IS:60
LCM IS:64
LCM IS:68
LCM IS:72
LCM IS:76
LCM IS:80
LCM IS:84
LCM IS:88
LCM IS:92
LCM IS:96

Enter fullscreen mode Exit fullscreen mode

 FINDING GCD:


package ifelse;

public class Gcd {
    public static void main(String[] args) {


    int no1=1000;
    int no2=500;

    int div=1;
  while (div < no1 && div < no2) {
      if(no1%div==0 && no2%div==0)
      System.out.println("GCD IS:"+div);
      div++;

  }
}
}

    OUTPUT:
       GCD IS:1
GCD IS:2
GCD IS:4
GCD IS:5
GCD IS:10
GCD IS:20
GCD IS:25
GCD IS:50
GCD IS:100
GCD IS:125
GCD IS:250


Enter fullscreen mode Exit fullscreen mode

Top comments (0)