913 views
3 3 votes
The value returned by the following function for foo(10) is ____

int foo(int x)
{
    if(x < 1)
        return 1;
    int sum = 0;
    for(i = 1; i <= x; i++)
    {
        sum += foo(x-i);
    }
    return sum;
}

1 Answer

Best answer
2 2 votes
With the help of recursion tree we can easily find the function value for some small numbers.

$\begin{align*} &\text{foo}(0) = 1 \\ &\text{foo}(1) = 1 \\ &\text{foo}(2) = 2 \\ &\text{foo}(3) = 4 \\ &\text{foo}(4) = 8 \\ &\text{foo}(5) = 16 \\ &\text{foo}(6) = 32 \\ \end{align*}$

We can see that, for $n > 0$ $ \;\;\;\; \text{foo}(n) = 2^{n-1} $

Therefore $\text{foo}(10) = 512$
• selected by
Position:
Show:

Related questions

0 0 votes
0 0 answers
206
206 views
5 5 votes
3 answers 3 answers
673
673 views
Vyshakh_Nambiar asked Dec 8, 2025
673 views
Should this question be invalid? t[4] Mentions 4 elements, but question assigns 5 elements.Because according to my knowledge accessing element t[4] (if we consider index ...
0 0 votes
1 answers 1 answer
499
499 views
Vartika Rawat asked Jul 17, 2025
499 views
In C language, if p1 and p2 both have data type int* and p1>p2 (both point to same array) then what is the data type of p1-p2?