Open In App

Algorithms | Recursion | Question 4

Last Updated : 28 Jun, 2021
Like Article
Like
Save
Share
Report

What does the following function do? 

C




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


(A)

x + y

(B)

x + x*y

(C)

x*y

(D)

xy



Answer: (C)

Explanation:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads