Skip to content

Commit 76d40c7

Browse files
authored
Merge pull request TheAlgorithms#436 from sumit18cs/patch-1
LCM.c
2 parents 6a120e1 + 54fe43e commit 76d40c7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

misc/LCM.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// C program to find LCM of two numbers
2+
/*
3+
suppose we have two numbers a and b.
4+
Property: Since product of LCM and GCD of two numbers are equal to product of that number itself.
5+
i.e, LCM(a,b)*GCD(a,b)=a*b.
6+
So,here we first find the GCD of two numbers and using above property we find LCM of that two numbers.
7+
*/
8+
#include <stdio.h>
9+
10+
// Recursive function to return gcd of a and b
11+
int gcd(int a, int b)
12+
{
13+
if (a == 0)
14+
return b;
15+
return gcd(b % a, a);
16+
}
17+
18+
// Function to return LCM of two numbers
19+
int lcm(int a, int b)
20+
{
21+
return (a*b)/gcd(a, b);
22+
}
23+
24+
// Driver program
25+
int main()
26+
{
27+
int a,b;
28+
printf("Enter two numbers to find their LCM \n");
29+
scanf("%d%d",&a,&b);
30+
printf("LCM of %d and %d is %d ", a, b, lcm(a, b));
31+
return 0;
32+
}
33+
/*
34+
Test Case1:
35+
a=15,b=20
36+
LCM(a,b)=60
37+
Test Case2:
38+
a=12,b=18
39+
LCM(a,b)=36
40+
*/

0 commit comments

Comments
 (0)