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

The last digit

How do you read the last digit of 4721? Your eyes just see it, but the computer sees one number. Think back to the remainder: divide 4721 by 10, and you get 472 with 1 left over.

That leftover is exactly the last digit. So the remainder after dividing by 10 always gives you the last digit of a number, whatever its size.

Remainder by 10: is the last digit.
In code
n = 4721
last = n % 10      # 1
Peeling digits off 4721
4
0
7
1
2
2
1
3
digit
n
4721
last digit
1

Start with 4721. The remainder after dividing by 10 is 1, so the last digit is 1.

Move 1 of 4

Dropping the last digit

The other half of the same division is the 472. Whole-number division by 10 throws away the leftover, and that cuts the last digit off.

Together the two tools take a number apart: use the remainder to read the last digit, then divide to drop it. To read the digit before the last one, drop first and read again. The tens digit of 4721 is the last digit of 472.

Divide by 10: to drop the last digit.
In code
n = 4721
rest = n // 10     # 472
tens = rest % 10   # 2