ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
int n = 1200, r = 0;
while (n) { r = r * 10 + n % 10; n /= 10; }
printf("%d", r);
  1. A. 2100
  2. B. 0021
  3. C. 21
  4. D. 12

Correct answer: C. 21

The loop builds the reversed number arithmetically: 0, 0, 2, 21. Leading zeros of the reversed digits contribute nothing to an int, so the result is 21, not 0021.

More C Programming questions

Exams that ask this topic