in Algorithms edited by
8,902 views
18 votes
18 votes

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$
in Algorithms edited by
8.9k views

3 Comments

Sum of the digits in the given decimal number 345, 3+4+5 = 12 is the correct option.
1
1
would the answer have been any different for signed int ?
0
0
No, it will remain same.
0
0

3 Answers

25 votes
25 votes
Best answer

Red colour represents return values.

Answer is 12.

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

so 5+4+3 = 12
2 votes
2 votes

Following recursive calls

Answer:

Related questions