The Gateway to Computer Science Excellence
+12 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 by Veteran (105k points)
edited by | 1.8k views
Sum of the digits in the given decimal number 345, 3+4+5 = 12 is the correct option.

3 Answers

+19 votes
Best answer

Red colour represents return values.

Answer is 12.

by Boss (23.9k 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)
+2 votes

Following recursive calls

by Active (2.5k points)

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
50,737 questions
57,321 answers
105,157 users