C Program To Find LCM of Two Numbers
Learn How To Find LCM of Two Numbers in C Programming Language. LCM is an abbreviated form for Least Common Multiple. This Code To Calculate LCM of Two Integers makes use of While Loop, For Loop and Modulus Operator. The Output for the program has been displayed at the bottom.
What is LCM?
A Least Common Multiple (LCM) of Two Integers is the Smallest Number which is a Multiple of Both the Numbers.
Example of LCM
The LCM of 4 and 3 is 12.
Formula To Find LCM of Two Integers
L.C.M = (x*y)/G.C.D
Must Read: Find LCM of N Numbers in C Programming
C Program To Find LCM of Two Numbers using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdio.h> int main() { int a, b, num1, num2, temp, gcd, lcm; printf("Enter First Integer:\t"); scanf("%d", &num1); printf("\nEnter Second Integer:\t"); scanf("%d", &num2); a = num1; b = num2; while (b > 0) { temp = b; b = a%b; a = temp; } gcd = a; lcm = (num1*num2)/gcd; printf("\nLeast Common Multiple of %d and %d = %d\n", num1, num2, lcm); printf("\n"); return 0; } |
Note: In While Loop’s condition while(b>0), you can also use while(b!=0). However, it doesn’t work always.
C Program To Calculate LCM of Integers using For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdio.h> int main() { int a, b, num1, num2, temp, gcd, lcm; printf("Enter First Integer:\t"); scanf("%d", &num1); printf("\nEnter Second Integer:\t"); scanf("%d", &num2); a = num1; b = num2; for (; b > 0;) { temp = b; b = a%b; a = temp; } gcd = a; lcm = (num1*num2)/gcd; printf("\nLeast Common Multiple of %d and %d = %d\n", num1, num2, lcm); printf("\n"); return 0; } |
Output

In case you find any error in the above C Program Code To Find LCM of Two Numbers or if you have any doubts, let us know about it in the Comment Section below.
Thanks for so many methods. Can we find LCM of 2 Numbers using Recursion in C Programming?
Yes. Almost all the loops can be efficiently converted into Recursive functions.
This is amazing man. I can now convert any For loop into While loop. Thanks for this amazing LCM program.
So basically we have to first find gcd and then lcm of the two numbers input by the user.