retagged by
8,199 views
16 votes
16 votes

Consider the following $\text{ANSI C}$ program

#include <stdio.h>
int foo(int x, int y, int q) 
    {
        if ((x<=0) && (y<=0))
        return q;
        if (x<=0)
        return foo(x, y-q, q);
        if (y<=0)
        return foo(x-q, y, q);
        return foo(x, y-q, q) + foo(x-q, y, q);
    }
int main( )
{
    int r = foo(15, 15, 10);
    printf(“%d”, r);
    return 0;
}

The output of the program upon execution is _________

retagged by

3 Answers

Best answer
22 votes
22 votes

Single child parent inherit the value of child, double child parents inherit the sum of the value of their children.

The function will return the value $60.$

selected by
9 votes
9 votes

Here is a traced out Recursive Tree for the above problem.

Answer: 60

3 votes
3 votes

Circles in the same color represent the repeated recursion part.

I have named the statements which will be executed at that point of time.

Answer is made considering the beginner point of view. Though, it can be done in shorter space as explained in the best answer.

Answer:

Related questions

11 votes
11 votes
4 answers
1
38 votes
38 votes
11 answers
3