Playing with Digits: Reversals and Palindromes

SA
StudyAI Editorial
Reviewed by StudyAI tutors
· Published Updated

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

Rustic wooden house number sign with colorful digits 1365 on weathered planks, outdoors.
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:

  1. Get the last digit: You can get the last digit of a number using the modulo operator (%). For example, 123 % 10 gives 3.
  2. 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.
  3. 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 // 10 gives 12.
  4. Repeat: Keep doing this until your original number becomes 0.

What is a Palindrome?

Artistic arrangement of wooden letters spelling 'WHAT' on a black background, offering creative inspiration.
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.

  1. Original number (N): 12321
    Reversed number (rev): 0 (start with zero)
    Store original (N_original): 12321

  2. Loop 1:

    • Last digit: 12321 % 10 = 1
    • rev = 0 * 10 + 1 = 1
    • N = 12321 // 10 = 1232
  3. Loop 2:

    • Last digit: 1232 % 10 = 2
    • rev = 1 * 10 + 2 = 12
    • N = 1232 // 10 = 123
  4. Loop 3:

    • Last digit: 123 % 10 = 3
    • rev = 12 * 10 + 3 = 123
    • N = 123 // 10 = 12
  5. Loop 4:

    • Last digit: 12 % 10 = 2
    • rev = 123 * 10 + 2 = 1232
    • N = 12 // 10 = 1
  6. Loop 5:

    • Last digit: 1 % 10 = 1
    • rev = 1232 * 10 + 1 = 12321
    • N = 1 // 10 = 0
  7. Loop ends because N is 0.

  8. Compare: Is N_original (12321) equal to rev (12321)? Yes!
    Therefore, 12321 is 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:

Flat lay of a spiral notebook and eraser on a pastel pink background with crossed out words.
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_number variable (it should start at 0).

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

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. Read the full notes above for the details.

Playing with Digits: Reversals and Palindromes is a core topic in NUMBER PLAY. Most exam papers test it via a mix of definitions, worked examples, and applied problems. The notes above cover the high-yield sub-topics, common pitfalls, and the kind of questions examiners typically set.

Yes. Every note in the StudyAI Campus Hub is free to read. Create a free account if you want to clone the full plan, generate your own notes from your textbook, or get AI-powered practice quizzes and flashcards.

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