All India Computer Science Engineering (CSE) Group

Predict output of following program
#include <stdio.h>
int fun(int n)
{
    if (n == 4)
       return n;
    else return 2*fun(n+1);
}
int main()
{
   printf("%d ", fun(2));
   return 0;
}
  • a)
    4
  • b)
    8
  • c)
    16
  • d)
    Runtime Error
Correct answer is option 'C'. Can you explain this answer?

Nitin Datta answered  •  17 hours ago
Program Explanation
The given program consists of a recursive function `fun(int n)` and a `main()` function that calls this recursive function with an initial value of 2.
Function Logic
- The function `fun(int n)` checks if `n` is equal to 4.
- If `n` equals 4, it returns `n` (which is 4).
- If `n` is not equal to 4, it makes a recursive call to itself with
... more
Fetching relevant content for you