This is the standard algorithm for converting a number from decimal to binary notation. Let us look at an example execution with the number 19
:
- the first divison yields a remainder of
1
with a quotient of 9
,
- the second divison yields a remainder of
1
with a quotient of 4
,
- the third divison yields a remainder of
0
with a quotient of 2
,
- the fourth divison yields a remainder of
0
with a quotient of 1
,
- the fifth divison yields a remainder of
1
with a quotient of 0
,
We push 1
, 1
, 0
, 0
, and 1
on to the stack in that order. We pop from the bottom, so the output we get is the numbers pushed on to the stack in the same order as they were pushed: 11001
— note that if we popped from the top, then the output would have reversed this order.
The correct answer is that the output is the reverse of the representation of n in binary. We omit here a formal justification of this fact, but you can convince yourself by combining your understanding of the standard conversion algorithm along with the behavior of the stack operations.