Recursion quiz predict the output

Predict the output of following program

#include <stdio.h>
int f(int n)
{
if(n <= 1)
return 1;
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1);
}

int main()
{
printf("%d", f(11));
return 0;
}

Now following calls will be made:

f(11) = f(5) + f(3) i.e 2+2=4

f(5) = f(2) + f(1) i.i. 1+1 =2

f(3) = f(1) + f(1) i.e. 1+1=2

f(2) = f(1) =1

Hence ans =4

is this the correct answer acc to me answer is 5

pleas confirm?

1 Like

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.