ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int f(int a, int b) {
    if (b == 0) return a;
    return f(b, a % b);
}
int main(void) { printf("%d", f(48, 18)); }
  1. A. 18
  2. B. 12
  3. C. 24
  4. D. 6

Correct answer: D. 6

This is the Euclidean algorithm for gcd. f(48,18)->f(18, 48%18=12)->f(12, 18%12=6)->f(6, 12%6=0)->returns a=6. gcd(48,18)=6.

More C Programming questions

Exams that ask this topic