1.5k views

Consider the following recursive C function that takes two arguments.

unsigned int foo(unsigned int n, unsigned int r) {
if (n>0) return ((n%r) + foo(n/r, r));
else return 0;
}


What is the return value of the function $\text{foo}$ when it is called as $\text{foo(345, 10)}$?

1. $345$
2. $12$
3. $5$
4. $3$
edited | 1.5k views
0
Sum of the digits in the given decimal number 345, 3+4+5 = 12 is the correct option.

Red color represents return values.

Answer is $12$.

edited
A) The function returns the sum of digits of the given number.

so 5+4+3 = 12

Following recursive calls

1
2