GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
185 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 (73.4k points)  
edited by | 185 views

2 Answers

+5 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.1k points)  
selected by
+2 votes

Final return value is 2 So option d

answered by Veteran (35.4k points)  
Top Users Jan 2017
  1. Debashish Deka

    8968 Points

  2. sudsho

    5326 Points

  3. Habibkhan

    4798 Points

  4. Bikram

    4532 Points

  5. Vijay Thakur

    4486 Points

  6. saurabh rai

    4222 Points

  7. Arjun

    4196 Points

  8. santhoshdevulapally

    3808 Points

  9. Sushant Gokhale

    3596 Points

  10. Kapil

    3486 Points

Monthly Topper: Rs. 500 gift card

19,212 questions
24,104 answers
53,151 comments
20,319 users