Java program to find LCM of two numbers

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

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 (LCM) of two numbers ‘a’ and ‘b’ is the smallest non zero positive integer that is divisible by both given numbers without remainder.

For example: LCM of 85 and 175 is 2975 and LCM of 26 and 93 is 2418.

java program to find LCM of two numbers

Here in the below java program, two numbers are stored in two variables ‘num1’ and ‘num2’ seperately.

Now set ‘lcm’ to largest of two numbers. Because ‘lcm’ cannot be less than largest number.

Also read – string constructors in java

In while loop we check if ‘lcm’ divides both ‘num1’ and ‘num2’ or not.

If both ‘num1’ and ‘num2’ divides, then ‘lcm’ is found. Else increment ‘lcm’ by 1.

Here’s java program to find LCM of two numbers using while loop and if statement.

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;
      }
   }
}


Output:

LCM of 85 and 175 is 2975.


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

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 + ".");
   }
}


Output:

LCM of 15 and 25 is 75.


to find hcf of two numbers

GCD of two numbers is the largest number that can exactly divide both numbers without remainder.

To find hcf of two numbers in java or to find gcd of two numbers in java first user enters two numbers using nextInt() method of Scanner class.

Also read – gcd and lcm of two numbers in java

These two numbers are stored in integer variables ‘number1’ and ‘number2’.

Then for loop is executed till integer variable ‘a’ is less than ‘number1’ and ‘number2’.

By doing this numbers between 1 and smallest of two user entered numbers(‘number1’ and ‘number2’) are iterated to find hcf.

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

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

Here’s the 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();
   }
}


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