UGC-NET | UGC NET CS 2016 July – II | Question 15

What is the value returned by the function f given below when n = 100?
int f (int n)
{ if (n = = 0) then return n;
else
return n + f(n-2);
}
(A) 2550
(B) 2556
(C) 5220
(D) 5520


Answer: (A)

Explanation: We have function:

int f (int n)
{ if (n = = 0) then return n;
else
return n + f(n-2);
}

we have to find output for n = 100
i.e
<>int f (100)
{ if (n = = 0) then return n; //failure//
else
return 100 + f (98)
{ if (n = = 0) then return n; //failure//
else
return 98 + f (96)
{ if (n = = 0) then return n; //failure//
else
return 96 + f (94)…………………..f(0);// It will be a AP series of 100 , 98, 96,………0// ;
};
};
}

Sum of series when we know first and last term and no of terms:
Sn = n / 2 * (first term + last term)
i.e. 51 / 2 * (100 + 0).
= 51 * 50
= 2550.
So, option (A) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.