Java program to find LCM of two numbers

Let’s learn java program to find LCM of two numbers.

How to find lcm of two numbers is one of the common interview question in java. Well, first let us understand what is LCM??

Least Common Multiple(LCM)

Least Common Multiple is one of the mathematical algorithms. LCM of two numbers ‘a’ and ‘b’ is the smallest non zero positive integer that is divisible by both given numbers, that is, ‘a’ and ‘b’.

java program to find LCM of two numbers

Here in the below java program, two numbers are stored in two variables. We have to set LCM to largest of two numbers. Because LCM cannot be less than largest number between two numbers.

Also read – string constructors in java

In while loop we check if LCM divides two numbers. If two numbers divides, LCM is found. Else increment LCM by 1.

Java program to find lcm of two numbers

Now let’s see how to find lcm. Here’s java program to find LCM of two numbers.

public class LCMOfTwoNumber
{
   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.printf("LCM of " + num1 + " and " + num2 + " is " + lcm + ".");
            break;
         }
         ++lcm;
      }
   }
}


Output:

LCM of 85 and 175 is 2975.


To calculate lcm two numbers in java we can use GCD. Here’s the formula,

LCM = (num1 * num2) / 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.printf("LCM of " + num1 + " and " + num2 + " is " + lcm + ".");
   }
}


Output:

LCM of 15 and 25 is 75.


hcf of two numbers in java or gcd of two numbers in java

To find hcf of two numbers in java or to find gcd of two numbers in java first user enters two numbers.

These two numbers is stored in integer variables ‘number1’ and ‘number2’ using nextInt() method of Scanner class.

In the next step for loop is executed till integer variable ‘a’ is less than ‘number1’ and ‘number2’.

Also read – lcm program in java using for loop

Here numbers between integer variable ‘a’ equal to 1 to smallest of two user entered numbers are iterated to find hcf of two numbers.

Now if ‘number1’ and ‘number2’ are divisible by integer variable ‘a’ hcf is set to number.

This loop iterates till largest number is found and which divides user entered numbers ‘number1’ and ‘number2’ without remainder.

Here’s the java program to find hcf 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();
   }
}


Output:

Please enter first number to find gcd or hcf: 96
Please enter second number to find gcd or hcf: 268
HCF of 96 and 268 is: 4