ProgrammingHardAccenture · Product company OA

What is printed?

What is printed?
int steps = 0;
int gcd(int a, int b) { steps++; return b == 0 ? a : gcd(b, a % b); }
int main(void) {
    int g = gcd(1071, 462);
    printf("%d %d", g, steps);
}
  1. A. 21 5
  2. B. 21 4
  3. C. 21 3
  4. D. 7 4

Correct answer: B. 21 4

The calls are (1071, 462) → (462, 147) → (147, 21) → (21, 0), which returns 21. That is 4 calls, including the final one with b = 0; '3' counts only the divisions.

More C Programming questions

Exams that ask this topic