Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sheetal369/ea33d34db81e16aa6ab698be4581de6c to your computer and use it in GitHub Desktop.

Select an option

Save sheetal369/ea33d34db81e16aa6ab698be4581de6c to your computer and use it in GitHub Desktop.
#include <stdio.h>
// C function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
// Driver Program
int main()
{
int x, y;
int a = 35, b = 15;
int g = gcdExtended(a, b, &x, &y);
printf("gcd(%d, %d) = %d", a, b, g);
printf("\n\nx = %d & y = %d", x, y) ;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment