ProgrammingHardGATE CS · Campus placements

int f(int n) { if (n == 0) return 0; return n % 2 + f(n / 2); } What is f(37)?

  1. A. 4
  2. B. 2
  3. C. 3
  4. D. 5

Correct answer: C. 3

f adds the lowest bit at each step, so it counts set bits. 37 = 100101 has three 1 bits, so f(37) = 3.

More C Programming questions

Exams that ask this topic