gcd.c

C

Public Domain

Greatest common denominator/divisor function in C

Download (right click, save as, rename as appropriate)

Embed

Tags:

C GCD math
1
2
3
4
5
6
7
8
int gcd(int x, int y) {
 int a, b;
 if (x<y) {a = y; b = x; }
 else if (x>y) {a = x; b = y; }
 else {return x; }
 do {int r = a % b; a = b; b = r; } while (b != 0);
 return a < 0 ? -a : a;
}