Java Program to find LCM of Two Numbers

Write a Java Program to find LCM of Two Numbers using While Loop and recursive method. According to Mathematics, LCM (Least Common Multiple) of two or more integers is the smallest positive integer that is divisible by the assigned integer values (without remainder). For example, the LCM of 2 and 3 is 6 because 6 is the smallest positive integer that is divisible by both 2 and 3.

NOTE: Some People call Java LCM as the Lowest Common Multiple.

Java Program to find LCM of Two Numbers using While Loop

This program for lcm of two numbers in java allows the user to enter two positive integer values. Next, in this Java program, we are using the While Loop and a temporary variable to calculate the LCM of those two positive integers.

// Java Program to find LCM of Two Numbers
import java.util.Scanner;

public class LCMofTwo1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Num1, Num2, Temp, GCD = 0, LCM = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the First Integer Value : ");
		Num1 = sc.nextInt();	
		
		System.out.print(" Please Enter the Second Integer Value : ");
		Num2 = sc.nextInt();
		
		int a = Num1;
		int b = Num2;
		
		while(Num2 != 0)
	    {
			Temp = Num2;
			Num2 = Num1 % Num2;
			Num1 = Temp;
	    }
		GCD = Num1;
		System.out.println("\n GCD of " + a + " and " + b + "  =  " + GCD);
		
		LCM = (a * b) / GCD;
		System.out.println("\n LCM of " + a + " and " + b + "  =  " + LCM);
		
	}
}

Java LCM output

 Please Enter the First Integer Value : 2
 Please Enter the Second Integer Value : 3

 GCD of 2 and 3  =  1

 LCM of 2 and 3  =  6

Java Program to Calculate LCM of Two Numbers without using Temp

This lcm of two numbers in java program calculates the Least Common Multiple without using any temporary variable.

// Java Program to find LCM of Two Numbers
import java.util.Scanner;

public class LCMofTwo2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Num1, Num2, GCD = 0, LCM = 0;
		sc = new Scanner(System.in);
	
		System.out.print(" Please Enter the First Integer Value : ");
		Num1 = sc.nextInt();	
	
		System.out.print(" Please Enter the Second Integer Value : ");
		Num2 = sc.nextInt();
	
		int a = Num1;
		int b = Num2;
	
		while(Num2 != 0)
	    {
			if(Num1 > Num2)
			{
				Num1 = Num1 - Num2;
			}
			else
			{
				Num2 = Num2 - Num1;
			}
	    }
		GCD = Num1;
		System.out.println("\n GCD of " + a + " and " + b + "  =  " + GCD);
	
		LCM = (a * b) / GCD;
		System.out.println("\n LCM of " + a + " and " + b + "  =  " + LCM);
	}
}
Java Program to find LCM of Two Numbers 2

Java Program to find LCM of Two Numbers using Recursive Method

This program for lcm of two numbers in java calculates the Greatest Common Divisor by calling HCFofTwo function recursively. Next, this Java program finds the Least Common Multiple from GCD.

// Java Program to find LCM of Two Numbers
import java.util.Scanner;

public class LCMofTwo3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Num1, Num2, GCD = 0, LCM = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the First Integer Value : ");
		Num1 = sc.nextInt();	
		
		System.out.print(" Please Enter the Second Integer Value : ");
		Num2 = sc.nextInt();
		
		GCD = HCFofTwo(Num1, Num2);		
		System.out.println("\n GCD of " + Num1 + " and " + Num2 + "  =  " + GCD);
		
		LCM = (Num1 * Num2) / GCD;
		System.out.println("\n LCM of " + Num1 + " and " + Num2 + "  =  " + LCM);
	}
	public static int HCFofTwo(int Num1, int Num2)
	{
		if(Num2 == 0)
		{
			return Num1;
		}
		else
		{
			return HCFofTwo(Num2, Num1 % Num2);
		}
	}
}

Java LCM output

 Please Enter the First Integer Value : 100
 Please Enter the Second Integer Value : 150

 GCD of 100 and 150  =  50

 LCM of 100 and 150  =  300