Skip to content

Commit 99410a9

Browse files
Create eucledian.c
1 parent c04ac92 commit 99410a9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Algorithms/eucledian.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// Euclidean Algorithm
3+
#include <stdio.h>
4+
5+
// C function for extended Euclidean Algorithm
6+
int gcdExtended(int a, int b, int *x, int *y)
7+
{
8+
// Base Case
9+
if (a == 0)
10+
{
11+
*x = 0;
12+
*y = 1;
13+
return b;
14+
}
15+
16+
int x1, y1; // To store results of recursive call
17+
int gcd = gcdExtended(b%a, a, &x1, &y1);
18+
*x = y1 - (b/a) * x1;
19+
*y = x1;
20+
21+
return gcd;
22+
}
23+
int main()
24+
{
25+
int x, y;
26+
int a = 35, b = 15;
27+
int g = gcdExtended(a, b, &x, &y);
28+
printf("gcd(%d, %d) = %d", a, b, g);
29+
return 0;
30+
}

0 commit comments

Comments
 (0)