ProgrammingHardProduct company OA

What is printed?

What is printed?
int f(int n, int b) { return n < b ? 1 : 1 + f(n / b, b); }
int main(void) { printf("%d %d %d", f(1000, 2), f(1000, 10), f(999, 10)); }
  1. A. 9 3 3
  2. B. 11 4 3
  3. C. 10 3 3
  4. D. 10 4 3

Correct answer: D. 10 4 3

f counts the digits of n in base b. 1000 lies between 2^9 = 512 and 2^10, so it needs 10 binary digits; in base 10 it has 4 digits and 999 has 3. '9' would be floor(log2 1000) without the +1.

More C Programming questions

Exams that ask this topic