According to Mathematics, the Least Common Multiple (LCM) of two or more integers is the smallest positive integer that is perfectly divisible by the given integer values, without remainder. For example, the LCM value of integer 2 and 3 is 12 because, 12 is the smallest positive integer that is divisible by both 2 and 3 (remainder is 0). In this article we will show you, How to write a C Program to find LCM of Two Numbers with example.

**TIP: **Some people will call the LCM as Lowest Common Multiple

## C Program to find LCM of Two Numbers using While Loop

This C program allows the user to enter two positive integer values and we are going to calculate the LCM of those two values using the While Loop.

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//C program to find LCM of two numbers #include <stdio.h> int main() { int Num1, Num2, max_Value; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); max_Value = (Num1 > Num2)? Num1 : Num2; while(1) //Alway True { if(max_Value % Num1 == 0 && max_Value % Num2 == 0) { printf("LCM of %d and %d = %d", Num1, Num2, max_Value); break; } ++max_Value; } return 0; } |

**OUTPUT**

## C Program to find LCM of Two Numbers using GCD

This C program allows the user to enter two positive integer values and we are going to calculate the Least Common Multiple of those two values using the While Loop.

**TIP:** The basic formula behind the Least Common Multiple is: LCM(a, b) = (a * b) / GCD. We already explained the GCD in our previous article. Please refer C Program to find GCD of Two Numbers for more information.

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//C program to find LCM of two numbers #include <stdio.h> int main() { int Num1, Num2, LCM, Temp, GCD; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); int a = Num1; int b = Num2; while (Num2 != 0) { Temp = Num2; Num2 = Num1 % Num2; Num1 = Temp; } GCD = Num1; printf("GCD of %d and %d = %d \n", a, b, GCD); LCM = (a * b) / GCD; printf("LCM of %d and %d = %d", a, b, LCM); return 0; } |

**OUTPUT**

## C Program to find LCM of Two Numbers using Recursion

This C program allows the user to enter two positive integer values and we are going to calculate the Lowest Common Multiple of those two values by calling the function recursively. Please refer Recursion in C for further reference

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//C program to find LCM of two numbers #include <stdio.h> long gcd(long x, long y); int main() { int Num1, Num2, GCD, LCM; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); GCD = gcd(Num1, Num2); LCM = (Num1 * Num2) / GCD; printf("LCM of %d and %d is = %d", Num1, Num2, LCM); return 0; } long gcd(long x, long y) { if (y == 0) { return x; } else { return gcd(y, x % y); } } |

**OUTPUT**

**ANALYSIS**

Following statement will find the Least Common Multiple using the traditional formula.

1 |
LCM = (Num1 * Num2) / GCD; |

When the compiler reaches to *long gcd(long x, long y)* line in main() program, the compiler will immediately jump to below function:

1 |
long gcd(long x, long y) |

In the above specified function, *return gcd(y, x % y)* statement will help the program to call the function Recursively with updated value and if you miss this statement then, after completing the first line it will terminate.

Lets see the If condition, if (y == 0) will check whether the number is equal to 0 or not. For Recursive functions it is very important to place a condition before using the function recursively otherwise, we will end up in infinite execution (Same like infinite Loop).

Please be careful 🙂

Thank you for Visiting Our Blog

## Leave a Reply