Write a C program to input two numbers from user and find LCM (Lowest Common Multiple) using loop. How to find LCM of two given numbers in C programming. Logic to find LCM of two number in C program.

**Example**

**Input**

Input number1: 12 Input number2: 30

**Output**

LCM = 60

## Required knowledge

Basic C programming, Conditional operator, If else, While loop, Infinite while loop

### What is LCM?

LCM is a smallest positive integer that exactly divides two or more numbers. For Example

## Logic to find LCM of two numbers

Step by step descriptive logic to find LCM of two numbers.

- Input two numbers from user. Store them in some variable say
`num1`and`num2`. - Find maximum between two numbers. Store the result in some variable, say
`max`. Maximum is used to generate next multiple which must be common to both. - If
`max`is exactly divisible by both numbers. Then you got your answer, store`max`to some variable say`lcm = max`

. If LCM is found then terminate from loop using break keyword. - If
`max`is not divisible by both numbers. Then generate next multiple of`max`. - Repeat steps 2 to 3 step till LCM is found.

## Program to find LCM of two numbers

```
/**
* C program to find LCM of any two numbers
*/
#include <stdio.h>
int main()
{
int i, num1, num2, max, lcm=1;
/* Input two numbers from user */
printf("Enter any two numbers to find LCM: ");
scanf("%d%d", &num1, &num2);
/* Find maximum between num1 and num2 */
max = (num1 > num2) ? num1 : num2;
/* First multiple to be checked */
i = max;
/* Run loop indefinitely till LCM is found */
while(1)
{
if(i%num1==0 && i%num2==0)
{
/*
* If 'i' divides both 'num1' and 'num2'
* then 'i' is the LCM.
*/
lcm = i;
/* Terminate the loop after LCM is found */
break;
}
/*
* If LCM is not found then generate next
* multiple of max between both numbers
*/
i += max;
}
printf("LCM of %d and %d = %d", num1, num2, lcm);
return 0;
}
```

Let us take a note of above program.

`while(1)`

is an infinite loop that runs indefinitely until LCM is found.- Do not confuse with the statement
`max = (num1 > num2) ? num1 : num2;`

. It is used to find maximum between two numbers.

Move a step forward and learn to find LCM using recursive approach.

Must learn - Program to find LCM using recursion.

Output

Enter any two numbers to find LCM: 12 30 LCM of 12 and 30 = 60

Happy coding 😉

<pre><code> ----Your Source Code---- </code></pre>