ProgrammingVery hardGATE CS · Competitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int A(int m, int n) {
    if (m == 0) return n + 1;
    if (n == 0) return A(m - 1, 1);
    return A(m - 1, A(m, n - 1));
}
int main(void) {
    printf("%d", A(2, 2));
}
  1. A. 7
  2. B. 4
  3. C. 29
  4. D. 6

Correct answer: A. 7

A(1,n) = n + 2 and A(2,n) = 2n + 3, so A(2,2) = 7, and the recursion always terminates. 4 is A(1,2), stopping one level early, 29 is A(3,2) = 2^5 - 3, and 6 misremembers A(2,n) as 2n + 2.

More C Programming questions

Exams that ask this topic