Skip to content

Commit 45b9551

Browse files
authored
Merge pull request #458 from AnupKumarGupta/GCD_of_two_numbers_in_C
Adds GCD of two numbers inC
2 parents 15afd7c + 60c54ad commit 45b9551

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

gcd/gcd.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)