Exit
  1. Learn
  2. Check
  3. Solve
  4. Reflect

Make room, then add

Picture typing a number on a calculator: you press 3, then 8, then 1, and the screen shows 381. Each new digit pushes everything already there one place to the left.

That is multiplying by 10 to make room, then adding the new digit. Starting from 0 you get 3, then 38, then 381.

Multiply by 10, then add: to put a digit on the end.
In code
r = 0
r = r * 10 + 3   # 3
r = r * 10 + 8   # 38
r = r * 10 + 1   # 381
Reverse 381
3
0
8
1
1
2
digit

Take 1: r = 0 × 10 + 1 = 1.

Move 1 of 3

Last out, first in

Now read the digits of another number from its right end and type each one into the calculator. The last digit is typed first, so it ends up first in the result, and the whole thing comes out reversed.

Zeros at the end of the original become leading zeros in the result, and leading zeros simply vanish: 120 reverses to 21.

Watch the size: reversing 2,147,483,647 gives 7,463,847,412, too big for a 32-bit int.
In code
r = 0
while n > 0:
    r = r * 10 + n % 10
    n //= 10
return r