Consider the following C code : What will be the output of the above code ?…

2024

Consider the following C code : What will be the output of the above code ?
#include
int temp = 0;
int fun(int x, int y)
{ int z;
temp++;
if (y==3)
return(x*x*x);
else
{ z=fun(x, y/3);
return(z*z*z);
}
} int main()
{fun(4, 81);
printf("%d", temp);
}

  1. A.

    3

  2. B.

    4

  3. C.

    5

  4. D.

    6

Attempted by 130 students.

Show answer & explanation

Correct answer: B

Answer: 4

Explanation:

  1. The global variable temp is incremented at the start of every call to fun (temp++).

  2. Starting from fun(4,81), each recursive call divides y by 3 until y == 3.

  3. Call sequence is: fun(4,81) → fun(4,27) → fun(4,9) → fun(4,3).

  4. There are 4 total calls, so temp is incremented 4 times and printf prints 4.

Note: The returned values (x*x*x and repeated cubing) do not affect the count stored in temp; only the number of calls matters.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor