Algorithms | Recursion | Question 2

Consider the following recursive function fun(x, y). What is the value of fun(4, 3)

filter_none

edit
close

play_arrow

link
brightness_4
code

int fun(int x, int y) 
{
  if (x == 0)
    return y;
  return fun(x - 1,  x + y);

chevron_right


(A) 13
(B) 12
(C) 9
(D) 10


Answer: (A)

Explanation: The function fun() calculates and returns ((1 + 2 … + x-1 + x) +y) which is x(x+1)/2 + y.

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.