What is the value returned by the recursive function ackermann(2,3) ? C int…

2026

What is the value returned by the recursive function ackermann(2,3) ?

C

int ackermann(int i, int j){
    if(i == 0) return 2*j;
    else if (j == 1) return 2;
    else return ackermann(i-1, ackermann(i, j-1)) ;
}

  1. A.

    16

  2. B.

    20

  3. C.

    25

  4. D.

    30

Attempted by 136 students.

Show answer & explanation

Correct answer: A

To find the value of ackermann(2,3), we trace the recursive calls step-by-step. The function follows these rules: if i=0 return 2*j; if j=1 return 2; otherwise return ackermann(i-1, ackermann(i, j-1)).\nStarting with ackermann(2,3), since i≠0 and j≠1, we compute ackermann(1, ackermann(2,2)).\nFirst, evaluate ackermann(2,2): this becomes ackermann(1, ackermann(2,1)). Since j=1 in the inner call, ackermann(2,1)=2. So ackermann(2,2) = ackermann(1,2).\nNow evaluate ackermann(1,2): this becomes ackermann(0, ackermann(1,1)). Since j=1 in the inner call, ackermann(1,1)=2. So ackermann(1,2) = ackermann(0,2).\nSince i=0 in the outer call, ackermann(0,2) = 2*2 = 4. Thus, ackermann(2,2)=4.\nSubstituting back: ackermann(2,3) = ackermann(1,4).\nEvaluating ackermann(1,4): this expands to ackermann(0, ackermann(1,3)).\nFollowing the same pattern, ackermann(1,3) = 8 and ackermann(1,2)=4. Actually, for i=1, the function computes 2^j.\nSo ackermann(1,4) = 2^4 = 16. Therefore, the final result is 16.",

Explore the full course: Tpsc Assistant Technical Officer