In this tutorial, you will learn **how to write a C program to find LCM of two numbers**.

## What is LCM?

LCM stands for **lowest common multiple**. LCM of two integers `num1`

and `num2`

is the smallest positive integer that is perfectly divisible by both the numbers, perfectly means the remainder is zero. For example, LCM of 15 and 50 is 150 because 150 is the smallest number that can be perfectly divisible by both 15 and 50.

## Example 1: Program to find LCM of two numbers using while loop

In this example, program prompts the user to input the two integers. The entered positive integers are stored in `num1`

and `num2`

respectively using `scanf()`

function.

The largest number between these two is stored in another variable `max`

. **The LCM cannot be less than max value**. Now we are running a while loop in which we are incrementing the value of max by 1 and on every iteration checking whether the `max`

is perfectly divisible by both numbers. If it is then displaying the `max`

value as the LCM of these two numbers else increase the `max`

value and check again.

```
#include <stdio.h>
int main() {
int num1, num2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
// greater number between num1 and num2 is stored in max
max = (num1 > num2) ? num1 : num2;
while (1) {
if (max % num1 == 0 && max % num2 == 0) {
printf("LCM of input numbers %d and %d is %d.", num1, num2, max);
break;
}
++max;
}
return 0;
}
```

**Output:**

## Example 2: Program to find LCM from GCD of two numbers

There is another way to find the LCM of two numbers using this formula `LCM = (num1*num2)/GCD`

. In this example, we are finding the GCD of two input numbers and then calculating the LCM using the mentioned mathematical formula.

```
#include <stdio.h>
int main() {
int num1, num2, i, gcd, lcm;
printf("Enter two positive integers: ");
//storing user input into num1 and num2
scanf("%d %d", &num1, &num2);
for (i = 1; i <= num1 && i <= num2; ++i) {
// check if the current value of i is a
// factor of both integers num1 and num2
if (num1 % i == 0 && num2 % i == 0)
gcd = i;
}
//Mathematical formula to calculate LCM from GCD
lcm = (num1 * num2) / gcd;
printf("LCM of two input numbers %d and %d is: %d.", num1, num2, lcm);
return 0;
}
```

**Output:**