Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Algorithms | Recursion | Question 9

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Predict output of following program 

C




#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
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Last Updated : 17 May, 2019
Like Article
Save Article
Similar Reads