Consider the following programme:

int recurrsion(int n)
{
static int r;
if (n < = 1) return 10; if (n > 5)
{
r = n;
return (f(n – 3) + 5);
}
return(f(n – 2) + 15);
}

What will be output if n = 10;

(A) 30
(B) 40
(C) 50
(D) 60


Answer: (C)

Explanation: For n = 10.
First, if the condition will fail it will move to second if condition and execute the following code.
Now function call for n = 7 will be called and, it again fails first if condition then it will move to second if and execute the given code.
Now first and second if condition will be failed so, execution will jump to the last statement and will continue execution for f(4) and f(2).
When it will come to f(0) then first if condition will execute and return 10.
Now f(2) will get 25 in return and f(2) will return 40 to f(4)
It will return 45 to f(7) which finally return 50 as output.

\"\"

Therefore, final output will be 50.
So, option (C) is correct.

Quiz of this Question


  • Last Updated : 13 Nov, 2018

Share your thoughts in the comments