ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
int g(int a, int b) { return a == b ? a : a > b ? g(a - b, b) : g(a, b - a); }
int main(void) { printf("%d", g(48, 18)); }
  1. A. 6
  2. B. 3
  3. C. 2
  4. D. 18

Correct answer: A. 6

Subtracting the smaller from the larger keeps the GCD unchanged: (48, 18) → (30, 18) → (12, 18) → (12, 6) → (6, 6), which returns 6.

More C Programming questions

Exams that ask this topic