Let’s learn java program to find LCM of two numbers.

## Java program to find lcm of two numbers

Well, first let us understand what is LCM??

**Least
Common Multiple(LCM)**

Least Common Multiple (LCM) of two numbers ‘a’ and ‘b’ is the smallest non zero positive integer that is divisible by both given numbers without remainder.

**For example:** LCM of 85 and 175 is 2975 and LCM of 26 and 93 is 2418.

Here in the below java program, given two numbers are stored in two variables ‘num1’ and ‘num2’ seperately.

Now set integer variable ‘lcm’ to largest of two numbers. Because ‘lcm’ cannot be less than largest number.

In while loop we check if ‘lcm’ divides two integer variables ‘num1’ and ‘num2’ or not. If both ‘num1’ and ‘num2’ divides, then ‘lcm’ is found.

Else increment ‘lcm’ by 1 and again test the condition. Here’s the program to find LCM of two numbers.

public class LCMOfTwoNumbers { public static void main(String[] args) { int num1 = 85, num2 = 175, lcm; lcm = (num1 > num2) ? num1 : num2; while(true) { if(lcm % num1 == 0 && lcm % num2 == 0) { System.out.println("LCM of " + num1 + " and " + num2 + " is " + lcm + "."); break; } ++lcm; } } }

**Output:**

LCM of 85 and 175 is 2975.

To find lcm of two numbers in java we can use GCD. Here’s the java program.

public class LCMUsingGCD { public static void main(String[] args) { int num1 = 15, num2 = 25, gcd = 1; for(int a = 1; a <= num1 && a <= num2; ++a) { if(num1 % a == 0 && num2 % a == 0) { gcd = a; } } int lcm = (num1 * num2) / gcd; System.out.println("LCM of " + num1 + " and " + num2 + " is " + lcm + "."); } }

**Output:**

LCM of 15 and 25 is 75.