Open In App
Related Articles

Algorithms | Recursion | Question 5

Improve Article
Improve
Save Article
Save
Like Article
Like

What does fun2() do in general? 

C




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

(A)

x*y

(B)

x+x*y

(C)

xy

(D)

yx



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.

int fun2(int a, int b): This function is also a recursive function that calculates the exponentiation of an integer a raised to the power of another integer b. It does this by repeatedly calling itself with the first argument a and decreasing b by 1 until b becomes 0 (base case).

Therefore, fun2() calculates the exponentiation of the first argument a raised to the power of the second argument b. It does this using the fun() function to perform multiplication in a recursive manner.

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