The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+11 votes
1.6k 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$
in Algorithms by Veteran (98.3k points)
edited by | 1.6k 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 colour represents return values.

Answer is 12.

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

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

Following recursive calls

by Active (2.3k points)
Answer:

Related questions

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
49,807 questions
54,711 answers
189,259 comments
79,685 users