GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
349 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
asked in Algorithms by Veteran (78.2k points)  
edited by | 349 views

2 Answers

+6 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

answered by Boss (7.2k points)  
selected by
+3 votes

Final return value is 2 So option d

answered by Veteran (37.1k points)  


Top Users Jul 2017
  1. Bikram

    4062 Points

  2. manu00x

    2464 Points

  3. Debashish Deka

    1850 Points

  4. joshi_nitish

    1658 Points

  5. Arjun

    1294 Points

  6. Hemant Parihar

    1184 Points

  7. Arnab Bhadra

    1112 Points

  8. Shubhanshu

    1054 Points

  9. Ahwan

    900 Points

  10. rahul sharma 5

    706 Points


24,022 questions
30,966 answers
70,346 comments
29,342 users