🔀📚Intermediate

Find LCM of Two Numbers

Find least common multiple

LCM (Least Common Multiple) is the smallest number divisible by both numbers.
Formula: LCM(a,b) = (a × b) / GCD(a,b)

Program Code

lcm.c
C
1#include <stdio.h>
2
3// Function to find GCD
4int gcd(int a, int b) {
5 while (b != 0) {
6 int temp = b;
7 b = a % b;
8 a = temp;
9 }
10 return a;
11}
12
13int main() {
14 int a, b, lcm;
15
16 printf("Enter two numbers: ");
17 scanf("%d %d", &a, &b);
18
19 // LCM = (a * b) / GCD(a, b)
20 lcm = (a * b) / gcd(a, b);
21
22 printf("LCM of %d and %d is %d\n", a, b, lcm);
23 return 0;
24}
Output

Enter two numbers: 12 18

LCM of 12 and 18 is 36