+10 votes
1.1k 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(513, 2)}$?

1. $9$
2. $8$
3. $5$
4. $2$

edited | 1.1k views

## 2 Answers

+17 votes
Best answer
The function returns the sum of digits in a binary representation of the given number

so $1+0+0+0+0+0+0+0+0+1 = 2$

Correct Answer: $D$
by Loyal (7.7k points)
edited
0
if you simulate this program with different values of n and r, notice that this function foo(n,r) gives the sum of digits of n(when it is converted to base r).

Like if n=513, r=14 then $(513)_{10}=(289)_{14}$ and 2+8+9=19 and this is the output of the program.
+9 votes

Final return value is 2 So option d

by Boss (38.3k points)
Answer:

+11 votes
3 answers
1
+13 votes
3 answers
2
+8 votes
3 answers
3
+14 votes
4 answers
4
+17 votes
2 answers
5
+15 votes
4 answers
6
+12 votes
3 answers
7