Algorithms | Recursion | Question 9

Predict output of following program

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


(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


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.