Signup/Sign In

Java Program to Calculate LCM of Two Numbers

In this tutorial, we will learn how to find the Least Common Multiple(LCM) of two numbers in java. The LCM of two integers is defined as the smallest positive integer that is perfectly divisible by both the numbers (without a remainder). But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.

Input: Enter the first number: 3

Enter the second number: 5

Output: LCM of the two numbers 3 and 5 is 15

Program 1: Java Program to Calculate the LCM of Two Numbers

In this program, we will see how to calculate the lcm of two numbers without using the gcd of the same two numbers.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two variables.
  4. Ask the user to initialize these variables.
  5. Declare a variable to store the lcm.
  6. Use the ternary operator to assign the largest number to that variable.
  7. Use a while loop to calculate the LCM.
  8. If the lcm is divisible by both the numbers, then display the lcm.
  9. Break the loop, if the condition satisfies.
  10. If the condition does not satisfy, then increment the lcm variable.
  11. Print the result.
  12. Stop.

//Java Program to Calculate the LCM of two numbers
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the first number: ");  
        int num1 = sc.nextInt();  
        System.out.println("Enter the second number: ");  
        int num2 = sc.nextInt();  
        int lcm = (num1 > num2) ? num1 : num2;
        // Always true
        while(true) 
        {
           if( lcm % num1 == 0 && lcm % num2 == 0 ) 
           {
               System.out.printf("The LCM of "+num1+" and "+num2+" is "+lcm);
               break;
            }
         ++lcm;
        }
   }  
}  


Enter the first number: 6
Enter the second number: 12
The LCM of 12 and 6 is 12

Program 2: Java Program to Calculate the LCM of Two Numbers

In this program, we will see how to calculate the lcm of two numbers using gcd of the same two numbers.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two variables.
  4. Ask the user to initialize these variables.
  5. Declare a variable to store the HCF and initialize it to 0.
  6. Use a for loop to calculate the GCD.
  7. If both the numbers are divisible by the loop variable, then set the number to the GCD.
  8. Continue the process till the largest number which divides both the numbers without remainder is found.
  9. Now, to calculate the lcm multiply both the numbers and divide it by the gcd.
  10. Print the result.
  11. Stop.
//Java Program to Calculate the LCM of two numbers
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the first number: ");  
        int num1 = sc.nextInt();  
        System.out.println("Enter the second number: ");  
        int num2 = sc.nextInt();  
        //Using GCD
        int gcd = 1;
        for(int i = 1; i <= num1 && i <= num2; ++i) 
        {
            // Checks if i is factor of both integers
            if(num1 % i == 0 && num2 % i == 0)
            gcd = i;
        }
        int lcm = (num1 * num2) / gcd;
        System.out.printf("The LCM of "+num1+" and "+num2+" is "+lcm);
            
   }  
}  


Enter the first number: 8
Enter the second number: 4
The LCM of 8 and 4 is 8



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.