**Summary:** In this programming example, we will learn to calculate the lcm of two numbers using the C language.

The **LCM** (Least Common Multiple) of any two numbers is the smallest possible integer that is divisible by both.

For example, lcm of 6 and 15 is 30.

Also, the LCM of any two number is always greater or equal to one of them.

## Method 1: Using While Loop

```
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers \n");
scanf("%d", &a);
scanf("%d", &b);
/*
*LCM is always greater than or equal to the-
*maximum of the given two numbers
*/
int lcm = (a > b) ? a : b;
//loop until divisible by both
while(1){
if(lcm%a==0 && lcm%b==0)
break;
lcm++;
}
//output the LCM
printf("LCM of %d and %d is: %d", a, b, lcm);
return 0;
}
```

**Output**:

`Enter two numbers
6
15
LCM of 6 and 15 is: 30`

In this method, we first store the maximum of the two numbers into the `lcm`

and inside while loop, we check if the `lcm`

is divisible by both the numbers or not.

If yes, then the current value of `lcm`

is the LCM of the input numbers otherwise we increment the value of `lcm`

(i.e. `lcm++`

) and repeat the process until lcm is divisible by both.

## Method 2: Using GCD

For any two numbers a and b, the following relationship holds true:

`a * b = LCM * GCD`

So we can also get LCM of any two numbers using the formula `(a*b)/GCD`

.

```
#include <stdio.h>
//function to find the GCD of two numbers
int findGCD(int a, int b){
int temp;
while(b){
temp = b;
b = a%b;
a = temp;
}
return a;
}
//main method
int main() {
int a, b;
printf("Enter two numbers \n");
scanf("%d", &a);
scanf("%d", &b);
//Step 1: First find GCD
int gcd = findGCD(a, b);
//Step 2: Put Value of GCD in the formula
int lcm = (a*b)/gcd;
//output the LCM
printf("LCM of %d and %d is: %d", a, b, lcm);
return 0;
}
```

**Output**:

`Enter two numbers
5
15
LCM of 5 and 15 is: 15`

In this tutorial, we learned to find the LCM of two numbers using the C programming language.