GateAI
Recursion

GATE CSE 2015 Set 2 · Q11

NAT1 markMedium

Time

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 _______.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 51

Recursion

We can compute the value returned by fun(n) recursively for increasing values of nn:

  1. For n=1n = 1: The base condition if (n == 1) triggers, returning:

    fun(1)=1\text{fun}(1) = 1
  2. For n=2n = 2: Initially x=1x = 1.

    • For k=1k = 1: x=1+fun(1)×fun(1)=1+(1×1)=2x = 1 + \text{fun}(1) \times \text{fun}(1) = 1 + (1 \times 1) = 2

    fun(2)=2\text{fun}(2) = 2
  3. For n=3n = 3: Initially x=1x = 1.

    • For k=1k = 1: x=1+fun(1)×fun(2)=1+(1×2)=3x = 1 + \text{fun}(1) \times \text{fun}(2) = 1 + (1 \times 2) = 3

    • For k=2k = 2: x=3+fun(2)×fun(1)=3+(2×1)=5x = 3 + \text{fun}(2) \times \text{fun}(1) = 3 + (2 \times 1) = 5

    fun(3)=5\text{fun}(3) = 5
  4. For n=4n = 4: Initially x=1x = 1.

    • For k=1k = 1: x=1+fun(1)×fun(3)=1+(1×5)=6x = 1 + \text{fun}(1) \times \text{fun}(3) = 1 + (1 \times 5) = 6

    • For k=2k = 2: x=6+fun(2)×fun(2)=6+(2×2)=10x = 6 + \text{fun}(2) \times \text{fun}(2) = 6 + (2 \times 2) = 10

    • For k=3k = 3: x=10+fun(3)×fun(1)=10+(5×1)=15x = 10 + \text{fun}(3) \times \text{fun}(1) = 10 + (5 \times 1) = 15

    fun(4)=15\text{fun}(4) = 15
  5. For n=5n = 5: Initially x=1x = 1.

    • For k=1k = 1: x=1+fun(1)×fun(4)=1+(1×15)=16x = 1 + \text{fun}(1) \times \text{fun}(4) = 1 + (1 \times 15) = 16

    • For k=2k = 2: x=16+fun(2)×fun(3)=16+(2×5)=26x = 16 + \text{fun}(2) \times \text{fun}(3) = 16 + (2 \times 5) = 26

    • For k=3k = 3: x=26+fun(3)×fun(2)=26+(5×2)=36x = 26 + \text{fun}(3) \times \text{fun}(2) = 26 + (5 \times 2) = 36

    • For k=4k = 4: x=36+fun(4)×fun(1)=36+(15×1)=51x = 36 + \text{fun}(4) \times \text{fun}(1) = 36 + (15 \times 1) = 51

    fun(5)=51\text{fun}(5) = 51

Thus, the return value of fun(5) is 51.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Recursion previous year question is Q11 of the GATE CSE 2015 Set 2 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Recursion in GATE CSE

Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 2 of them are 1-mark NAT questions like this one, and GATE CSE 2015 Set 2 has 2.

How GATE tests Recursion

More from GATE CSE 2015 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2015 Set 2 · Q11 · NAT

Consider the following C function. int fun(int n) { int x=1, k; if (n==1) return x; for (k=1; k The return value of fun(5) is …

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.