Skip to content
Related Articles
Open in App
Not now

Related Articles

Algorithms | Recursion | Question 9

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 17 May, 2019
Improve Article
Save Article

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


Answer: (C)

Explanation:

Fun(2) = 2 * Fun(3) and Fun(3) = 2 * Fun(4) ....(i)
Fun(4) = 4 ......(ii)
From equation (i) and (ii),
Fun(2) = 2 * 2 * Fun(4)
Fun(2) = 2 * 2 * 4
Fun(2) = 16. 

So, C is the correct answer


Quiz of this Question

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!