basics · intermediate · ~15 min
Write a small reusable function with a loop.
Implement int gcd(int a, int b) returning the greatest common divisor of two non-negative integers (Euclid's algorithm). gcd(x,0)==x.
int gcd(int a, int b) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.