LCM in java – Java Program to Find LCM of two Numbers

LCM in java: Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Program to Find LCM of two Numbers

In this article we will learn different ways to find LCM between two numbers.

LCM:

LCM (Least Common Multiple) is the smallest positive number which is divisible by both the given numbers. Or we can simply that smallest positive number which is the multiple of both the values.

For example multiples of 5 and 7 are:

5 -> 5, 10, 15, 20, 25, 30, 35, 40 .....
7 -> 7, 14, 21, 28, 35, 42, 49 ......

Since 35 is the number which is the smallest multiple of 5 and 7. So we can say that LCM of 5 and 7 is 35.

Now our aim is to find LCM between two numbers. To implement this, we can use different approaches. Let’s see one by one.

Let’s see the approaches one by one.

Method-I : Find LCM of two numbers using While Loop

LCM of two numbers can be calculated using While Loop. In while loop we will check if any number is divisible by both the given numbers. If any found that is the LCM and we will come out of the loop.

Approach:

  • Declare and initialize two variables say num1 and num2 for storing two numbers.
  • By using ternary operator, store the bigger value between the two numbers in a variable say lcm which reduce the number of iterations.
  • Take WHILE loop which to calculate LCM.
  • Using IF condition check if lcm is divisible by both the numbers, then print lcm. If does not satisfy increment lcm.
  • Come out of the loop using BREAK if condition satisfies.

Program:

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

    int num1 = 15, num2 = 21, lcm;

    // biggest number between the two stored in lcm
    lcm = (num1 > num2) ? num1 : num2;

    // to minimize the loop iterations we start by checking both the numbers from biggest number i.e. lcm
    while(true)
    {
    // keep on checking lcm with num1 and num2 
    // until both num1 and num2 are perfectly divisible with lcm
      if( lcm % num1 == 0 && lcm % num2 == 0 ) 
      {
        System.out.printf("The LCM is %d",lcm);
        break;
      }
      ++lcm;
    }
  }
} 
Output:
The LCM is 105

Method-II : Find LCM of two numbers using FOR loop

We can find LCM of two numbers using FOR loop. We will iterate for loop which will iterate till we get a minimum value which is divisible by both the given numbers, which is the required LCM.

Approach

  • Declare and initialize two variables say larg and lc.
  • Declare two variables say num1 and num2 for which you want to calculate LCM
  • Take user input using Scanner class and store two numbers in num1 and num2.
  • Using ternary operator store the larger values between the two numbers in larg. We took larg variable so that we can decrease iterations of for loop.
  • Iterate for loop from 0 till larg.
  • Inside IF condition check if larg is divisible by both the numbers, if found that is the LCM.

Program:

import java.util.Scanner;
 
class CalcLCM
{
 public static void main(String args[])
 {
 long num1,num2,larg,lc=1;
 Scanner sc = new Scanner(System.in);
 // ask users to enter two numbers
 System.out.println("Enter two numbers : ");
 num1 = sc.nextInt();
 num2 =sc.nextInt(); 
 // store maximum value between num1 and num2
 larg=num1>num2?num1:num2;
     for(int i=0;i<larg;i++)
     {
    // whenever two number are divisible by larg it will assigned to lc
         if(larg%num1==0 && larg%num2==0)
         {
         	lc=larg;
         	// come out of the loop
             break;
         }
         larg++;
     }
     // print the lcm
     System.out.println("LCM of the two numbers = "+lc);
 }
}
Output:

Enter two numbers :
65
25

Method-III : Find LCM of two numbers using GCD

We can also find LCM between two numbers by GCD.

Greatest Common Divisor (GCD) of two numbers is largest integer dividing both the integer.

LCM is equal to multiplication of both the numbers divide by GCD.

Approach:

  • Create instance of Scanner class to take inputs.
  • Declare two variables num1 and num2 and store two numbers taking user input.
  • Declare and initialize variable say gcd.
  • In for loop iterate say j where j<= both the numbers.
  • Inside for loop take IF condition which checks if both the numbers are divisible by j. If found store in gcd. This is the GCD.
  • Then calculate LCM by doing num1*num2 divide by gcd.

Program:

import java.util.Scanner;  
public class CalcLCM   
{  
    public static void main(String[] args)    
    {
        //Ask input from user
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the first number: ");  
        long num1 = sc.nextInt();  
        System.out.println("Enter the second number: ");  
        long num2 = sc.nextInt();  
        //Declare and initialize gcd
        long gcd = 1;
        for(long j = 1; j <= num1 && j <= num2; ++j) 
        {
            // checks if j is the common factor of two number
            if(num1 % j == 0 && num2 % j == 0)
            gcd = j;
        }
        long lcm = (num1 * num2) / gcd;
        System.out.println("The LCM of the two numbers = "+lcm);
            
   }  
} 
Output:

Enter the first number: 
16
1Enter the second number: 
19
The LCM of the two numbers = 304

Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.

Related Java Decision Making and Loop Programs: