Greatest common divisor.

This commit is contained in:
fsharpasharp 2017-10-19 10:04:14 +02:00
parent 98005e3fde
commit a82550d66f

20
misc/GCD.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
// Euclid's algorithm
int GCD(int x, int y) {
if (x == y || y == 0)
return x;
if (x == 0)
return y;
if (x > y)
return GCD(x-y, y);
else
return GCD(x, y-x);
}
int main() {
int a, b;
printf("Input two numbers:\n");
scanf("%d %d", &a, &b);
printf("Greatest common divisor: %d\n", GCD(a, b));
}