Skip to content

Commit bc304c5

Browse files
Adds GCD of two numbers inC
1 parent 6470ee5 commit bc304c5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

gcd/gcd.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
// Recursive function to return gcd of a and b
4+
int gcd(int a, int b)
5+
{
6+
// Everything divides 0
7+
if (a == 0 || b == 0)
8+
return 0;
9+
10+
// base case
11+
if (a == b)
12+
return a;
13+
14+
// a is greater
15+
if (a > b)
16+
return gcd(a-b, b);
17+
return gcd(a, b-a);
18+
}
19+
20+
// Driver program to test above function
21+
int main()
22+
{
23+
int a,b;
24+
scanf("%d%d",&a,&b);
25+
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
26+
return 0;
27+
}
28+
29+

0 commit comments

Comments
 (0)