Farangi
3
Q:

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;
      }
   }
}
1
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 + ".");
   }
}
1
java program to find hcf of two numbers or gcd of two numbers
import java.util.Scanner;
public class HCFOfTwoNumbers 
{
   public static void main(String[] args) 
   {
      int hcf = 1;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number to find gcd or hcf: ");
      int number1 = sc.nextInt();
      System.out.println("Please enter second number to find gcd or hcf: ");
      int number2 = sc.nextInt();
      for(int a = 1; a <= number1 && a <= number2; ++a)
      {
         if(number1 % a == 0 && number2 % a == 0)
            hcf = a;
      }
      System.out.println("HCF of " + number1 + " and " + number2 + " is: " + hcf);
      sc.close();
   }
}
1

New to Communities?

Join the community