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);
}
- A.
3
- B.
4
- C.
5
- D.
6
Attempted by 130 students.
Show answer & explanation
Correct answer: B
Answer: 4
Explanation:
The global variable temp is incremented at the start of every call to fun (temp++).
Starting from fun(4,81), each recursive call divides y by 3 until y == 3.
Call sequence is: fun(4,81) → fun(4,27) → fun(4,9) → fun(4,3).
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.