# Java program to find lcm of 2 numbers

Hey guys!! Welcome to flower brackets blog. Well, today you will be learning java program to find lcm of 2 numbers.

Well, first let us understand what this term mean??

Least Common Multiple(LCM)

Least Common Multiple of two integers a and b is the smallest non zero positive integer that is divisible by both a and b.

Here in the below java program, two numbers are stored in two variables. We have to set lcm to largest of two numbers since lcm cannot be less than largest number between two numbers.

In while loop we check if lcm divides two numbers. If two numbers divides, LCM is found. Else increment lcm by 1.

Now let’s see java program to find LCM of two numbers,

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

```

Output:

LCM of 15 and 25 is 75.

To find lcm of two numbers we can use gcd. Here’s the formula,

LCM = (num1 * num2) / GCD

```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.printf("LCM of " + num1 + " and " + num2 + " is " + lcm + ".");
}
}

```

Output:

LCM of 15 and 25 is 75.