The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+11 votes
1.2k 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$
asked in Algorithms by Veteran (101k points)
edited by | 1.2k views
0
Sum of the digits in the given decimal number 345, 3+4+5 = 12 is the correct option.

3 Answers

+17 votes
Best answer

Red color represents return values.

Answer is $12$.

answered by Boss (22.8k points)
edited by
+11 votes
A) The function returns the sum of digits of the given number.

so 5+4+3 = 12
answered by Active (3.9k points)
0 votes

Following recursive calls

answered by Active (2.1k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

39,848 questions
46,813 answers
141,149 comments
59,061 users