-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathlcm.c
37 lines (34 loc) · 848 Bytes
/
lcm.c
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
26
27
28
29
30
31
32
33
34
35
36
37
// C program to find LCM of two numbers
/*
suppose we have two numbers a and b.
Property: Since product of LCM and GCD of two numbers are equal to product
of that number itself. i.e, LCM(a,b)*GCD(a,b)=a*b. So,here we first find the
GCD of two numbers and using above property we find LCM of that two numbers.
*/
#include <stdio.h>
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to return LCM of two numbers
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
// Driver program
int main()
{
int a, b;
printf("Enter two numbers to find their LCM \n");
scanf("%d%d", &a, &b);
printf("LCM of %d and %d is %d ", a, b, lcm(a, b));
return 0;
}
/*
Test Case1:
a=15,b=20
LCM(a,b)=60
Test Case2:
a=12,b=18
LCM(a,b)=36
*/