Open In App
Related Articles

Algorithms | Recursion | Question 4

Improve Article
Save Article
Like Article

What does the following function do? 


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


x + y


x + x*y





Answer: (C)


int fun(int x, int y): This function is a recursive function that calculates the product of two integers x and y. It does this by repeatedly adding x to itself y times. If y is 0, the function returns 0 (base case). Otherwise, it returns x plus the result of calling itself with x and y-1.

Therefore, The function adds x to itself y times which is x*y.

Hence (C) is the correct answer.

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

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads