GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
297 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 (77.2k points)  
edited by | 297 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 (36.7k points)  


Top Users May 2017
  1. akash.dinkar12

    3302 Points

  2. pawan kumarln

    1776 Points

  3. Bikram

    1646 Points

  4. sh!va

    1640 Points

  5. Arjun

    1396 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1142 Points

  8. Angkit

    1044 Points

  9. LeenSharma

    1000 Points

  10. Arunav Khare

    754 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    732 Points

  2. Arnab Bhadra

    402 Points

  3. pawan kumarln

    402 Points

  4. bharti

    304 Points

  5. LeenSharma

    238 Points


22,823 questions
29,142 answers
65,209 comments
27,666 users