GATE | GATE-CS-2015 (Set 2) | Question 21
Consider the following C function.
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.
int fun (int n){ int x=1, k; if (n==1) return x; for (k=1; k < n; ++k) x = x + fun(k) * fun(n – k); return x;} |
The return value of fun(5) is __________.
(A) 0
(B) 26
(C) 51
(D) 71
Answer: (C)
Explanation:
fun(5) = 1 + fun(1) * fun(4) + fun(2) * fun(3) +
fun(3) * fun(2) + fun(4) * fun(1)
= 1 + 2*[fun(1)*fun(4) + fun(2)*fun(3)]
Substituting fun(1) = 1
= 1 + 2*[fun(4) + fun(2)*fun(3)]
Calculating fun(2), fun(3) and fun(4)
fun(2) = 1 + fun(1)*fun(1) = 1 + 1*1 = 2
fun(3) = 1 + 2*fun(1)*fun(2) = 1 + 2*1*2 = 5
fun(4) = 1 + 2*fun(1)*fun(3) + fun(2)*fun(2)
= 1 + 2*1*5 + 2*2 = 15
Substituting values of fun(2), fun(3) and fun(4)
fun(5) = 1 + 2*[15 + 2*5] = 51 


