In this article we will show you, How to 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 perfectly divisible by the given 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 LCM as Lowest Common Multiple.

## Java Program to find LCM of Two Numbers using While Loop

This program allows the user to enter two positive integer values. Next, 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); } }

**OUTPUT**

## Java Program to find LCM of Two Numbers without using Temp

This program will calculate 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); } }

**OUTPUT**

## Java Program to find LCM of Two Numbers using Recursive Method

This program will calculate the Greatest Common Divisor by calling HCFofTwo function recursively. Next, it will find 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); } } }

**OUTPUT**

Thank you for Visiting Our Blog