PUBLISHED ON: APRIL 16, 2021
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:
- Start
- Create an instance of the Scanner class.
- Declare two variables.
- Ask the user to initialize these variables.
- Declare a variable to store the lcm.
- Use the ternary operator to assign the largest number to that variable.
- Use a while loop to calculate the LCM.
- If the lcm is divisible by both the numbers, then display the lcm.
- Break the loop, if the condition satisfies.
- If the condition does not satisfy, then increment the lcm variable.
- Print the result.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare two variables.
- Ask the user to initialize these variables.
- Declare a variable to store the HCF and initialize it to 0.
- Use a for loop to calculate the GCD.
- If both the numbers are divisible by the loop variable, then set the number to the GCD.
- Continue the process till the largest number which divides both the numbers without remainder is found.
- Now, to calculate the lcm multiply both the numbers and divide it by the gcd.
- Print the result.
- 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