ProgrammingHardTCS NQT · Infosys pattern

What is printed? (binary string of n using recursion, division by 2)

What is printed? (binary string of n using recursion, division by 2)
void toBinary(int n) {
    if (n == 0) return;
    toBinary(n / 2);
    printf("%d", n % 2);
}
int main(void) {
    toBinary(13);
}
  1. A. 1011
  2. B. 1101
  3. C. 13
  4. D. 0000

Correct answer: B. 1101

The recursive call happens BEFORE the printf, so digits are produced in most-significant-first order as the stack unwinds. 13 in binary is 1101, and this standard recursion prints exactly that.

More C Programming questions

Exams that ask this topic