Answers for "lcm of a number in java"

1

Java program to find LCM of two numbers

public class LCMOfTwoNumbers
{
   public static void main(String[] args)
   { 
      int num1 = 85, num2 = 175, lcm;
      lcm = (num1 > num2) ? num1 : num2;
      while(true)
      {
         if(lcm % num1 == 0 && lcm % num2 == 0)
         {
            System.out.println("LCM of " + num1 + " and " + num2 + " is " + lcm + ".");
            break;
         }
         ++lcm;
      }
   }
}
Posted by: Guest on October-30-2020
1

LCM with java

/*
Easiest but slowest method
*/
class LCM 

{ 

    static void find_LCM(int num1, int num2) 

    { 

        System.out.println("Number 1: " + num1); 

        System.out.println("Number 2: " + num2); 

        long check_num = 1; 

        while (true) 

        { 

            if ((check_num%num1) == 0 && (check_num%num2) == 0) 

            { 

                 long LCM = check_num; 

                 System.out.println("The LCM is: " + LCM); 

                 break; 

            } 

            else  

                ++check_num; 

        }  

    } 

}
Posted by: Guest on July-12-2021
1

Java program to find LCM of two numbers

calculate lcm two numbers in java we can use GCD
public class LCMUsingGCD 
{
   public static void main(String[] args) 
   {
      int num1 = 15, num2 = 25, gcd = 1;
      for(int a = 1; a <= num1 && a <= num2; ++a)
      {
         if(num1 % a == 0 && num2 % a == 0)
         {
            gcd = a;
         }
      }
      int lcm = (num1 * num2) / gcd;
      System.out.println("LCM of " + num1 + " and " + num2 + " is " + lcm + ".");
   }
}
Posted by: Guest on October-30-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language