GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
410 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 (87.4k points)  
edited by | 410 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.3k points)  
selected by
+3 votes

Final return value is 2 So option d

answered by Veteran (37.5k points)  


Top Users Sep 2017
  1. Habibkhan

    8796 Points

  2. rishu_darkshadow

    3572 Points

  3. Warrior

    2914 Points

  4. Arjun

    2840 Points

  5. A_i_$_h

    2550 Points

  6. manu00x

    2268 Points

  7. nikunj

    1990 Points

  8. Bikram

    1874 Points

  9. makhdoom ghaya

    1820 Points

  10. SiddharthMahapatra

    1718 Points


26,346 questions
33,928 answers
80,524 comments
31,231 users