Playing with Digits: Reversals and Palindromes
From the NUMBER PLAY curriculum
Playing with Digits: Reversals and Palindromes
TL;DR
Reversing digits means rearranging a number's digits in the opposite order. A palindrome is a number that reads the same forwards and backward. We'll explore how to reverse numbers and identify palindromes using simple arithmetic.
1. The Mental Model
Imagine you have a number written on a strip of paper. Reversing it is like flipping that strip over to read it from right to left. A palindrome is simply a number that looks the same whether you flip the paper or not.
2. The Core Material
When we talk about reversing digits, we're not just flipping the number's sign or its position. We're actually rearranging the individual digits that make up the number. For example, reversing 123 gives you 321.
How to Reverse a Number Digit by Digit

Photo by Chris F on Pexels
To reverse a number, you'll generally extract its last digit, then "build" the reversed number using these extracted digits. It's a bit like taking bricks off one end of a wall and adding them to the other end to build a new wall.
Here's the process:
- Get the last digit: You can get the last digit of a number using the modulo operator (
%). For example,123 % 10gives3. - Add to reversed number: Take this last digit and add it to your growing "reversed number." Each time you add a digit, you'll need to shift the existing reversed number to the left (multiply by 10) to make space.
- Remove the last digit: To get rid of the digit you just processed, divide your original number by 10 using integer division (
//). For example,123 // 10gives12. - Repeat: Keep doing this until your original number becomes 0.
What is a Palindrome?

Photo by Ann H on Pexels
A palindrome is a number that remains the same when its digits are reversed.
* 121 is a palindrome because reversing it gives 121.
* 54545 is a palindrome because reversing it gives 54545.
* 123 is NOT a palindrome because reversing it gives 321.
To check if a number is a palindrome, you simply reverse it and then compare the reversed number to the original number. If they're the same, it's a palindrome!
graph TD
A["Start: Pick a Number (N)"] --> B{"Is N > 0?"}
B -- Yes --> C["Extract Last Digit (N % 10)"]
C --> D["Build Reversed Number (rev = rev * 10 + last_digit)"]
D --> E["Remove Last Digit (N = N // 10)"]
E --> B
B -- No --> F["Compare Original Number (N_original) with Reversed Number (rev)"]
F -- N_original == rev --> G["It's a Palindrome!"]
F -- N_original != rev --> H["It's Not a Palindrome."]
G --> I["End"]
H --> I
3. Worked Example
Let's reverse the number 12321 and check if it's a palindrome.
-
Original number (N):
12321
Reversed number (rev):0(start with zero)
Store original (N_original):12321 -
Loop 1:
- Last digit:
12321 % 10=1 rev = 0 * 10 + 1=1N = 12321 // 10=1232
- Last digit:
-
Loop 2:
- Last digit:
1232 % 10=2 rev = 1 * 10 + 2=12N = 1232 // 10=123
- Last digit:
-
Loop 3:
- Last digit:
123 % 10=3 rev = 12 * 10 + 3=123N = 123 // 10=12
- Last digit:
-
Loop 4:
- Last digit:
12 % 10=2 rev = 123 * 10 + 2=1232N = 12 // 10=1
- Last digit:
-
Loop 5:
- Last digit:
1 % 10=1 rev = 1232 * 10 + 1=12321N = 1 // 10=0
- Last digit:
-
Loop ends because
Nis0. -
Compare: Is
N_original(12321) equal torev(12321)? Yes!
Therefore,12321is a palindrome.
Here's how you might write this in Python:
def is_palindrome(number):
original_number = number
reversed_number = 0
while number > 0:
last_digit = number % 10
reversed_number = reversed_number * 10 + last_digit
number = number // 10
return original_number == reversed_number
print(is_palindrome(12321)) # Output: True
print(is_palindrome(12345)) # Output: False
print(is_palindrome(7)) # Output: True
4. Key Takeaways
- Reversing a number involves extracting its digits one by one from right to left.
- The modulo operator (
% 10) gives you the last digit of an integer. - Integer division (
// 10) removes the last digit from an integer. - To build a reversed number, you multiply your current reversed number by 10 and add the new digit.
- A number is a palindrome if it reads the same forwards and backward.
- You can check for a palindrome by comparing the original number to its reversed version.
Common Mistakes to Avoid:

Photo by KATRIN BOLOVTSOVA on Pexels
- Forgetting to store the original number before you start reversing, as the reversal process modifies the original.
- Accidentally using floating-point division (
/) instead of integer division (//), which can lead to decimals and errors. - Not handling single-digit numbers correctly (they are always palindromes).
- Incorrectly initializing the
reversed_numbervariable (it should start at0).
5. Now Try It
Write a short Python function called reverse_number(n) that takes an integer n and returns its reversed version. For example, reverse_number(456) should return 654. Then, use this function to find the first 5 numbers greater than 100 that are palindromes and print them.
What success looks like: You'll have a function that correctly reverses any positive integer, and a list of five palindrome numbers (like 101, 111, 121, etc.).
Frequently asked about Playing with Digits: Reversals and Palindromes
More from NUMBER PLAY
Get the full NUMBER PLAY curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account