Open In App

GATE | GATE-CS-2015 (Set 2) | Question 21

Consider the following C function. 




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 

Hence Option(C) is the correct answer.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :