Summary: In this tutorial, we will learn how to calculate lcm of two numbers in C.

The LCM (Least Common Multiple) of two numbers is the smallest possible integer that is divisible by both. For example, lcm of 6 and 15 is 30.

LCM using While Loop

Output

In this program, the user enters the two numbers a and b.

The maximum of the two numbers is stored into lcm because LCM of any two numbers is always greater than or equal to the maximum of the given two numbers.

Next, inside the 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 we get the least common multiple value.

LCM using GCD

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

So we can get LCM using the formula (a*b)/GCD.

Output

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

Leave a Reply