/* * Program to compute GCD using recursive version of Euclid's algorithm . */ #include #include int gcd(int a, int b); int main(void) { int a, b; printf("enter two non-negative integers, not both zero:\n"); if (scanf("%d %d", &a, &b) != 2) { printf("invalid input\n"); return EXIT_FAILURE; } if ((a < 0) || (b < 0) || ((a == 0) && (b == 0))) { printf("invalid input\n"); return EXIT_FAILURE; } int result = (a >= b) ? gcd(a, b) : gcd(b, a); printf("gcd of %d and %d is %d\n", a, b, result); return EXIT_SUCCESS; } /* returns gcd of a and b, assuming a >= b and a > 0 */ /* * note that the algorithm actually works fine if a < b, or if * either is negative, but I wanted the code to match the * description in the assignment of the algorith. */ int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a%b); } }