Growing Reverse Number Pattern in Python

What You’ll Learn
How to print a growing reverse number pattern in Python. Starting from the last digit, we build the reversed number step-by-step and print it at each step.
This pattern is a practical way to understand digit extraction using % 10 and digit removal using // 10.
⭐ Pattern Output
For num = 86523, the pattern looks like this:
3
32
325
3256
32568Complete Python Program
Extract the last digit, append it to reverse, print it, and then remove the digit from num.
num = 86523
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print(reverse)
num //= 10🧠 How It Works
Initialize variables
num holds the remaining digits, and reverse stores the growing reverse value.
Extract the last digit
num % 10 gives the last digit of the current number.
Append digit to reverse
reverse = reverse * 10 + digit shifts existing digits left and adds the new digit.
Remove the last digit
num //= 10 discards the last digit so we can process the next one.
Growing reverse
Each iteration adds one digit, so the loop runs once per digit (\(O(d)\)).
Variation — User Input Version
Let the user enter a number and print the same growing reverse pattern.
num = int(input("Enter a number: "))
num = abs(num)
reverse = 0
if num == 0:
print(0)
else:
while num != 0:
reverse = reverse * 10 + (num % 10)
print(reverse)
num //= 10💡 Tips for Enhancement
Try These
- Skip printing leading zeros by ignoring digits until the first non-zero digit
- Print both
digitandreverseeach step for learning - Use this technique to reverse a number completely
- Try building the pattern as strings instead of integers
- Handle very large integers (Python supports big ints by default)
Avoid
- Using floating-point division (always use
//) - Forgetting to update
num(infinite loop) - Assuming input is always positive without handling negatives
- Confusing this pattern with printing suffixes (this prints reverse prefixes)
Key Takeaways
% 10 extracts the last digit; // 10 removes it.
reverse = reverse * 10 + digit appends a digit to the right.
Printing reverse each step produces the growing pattern.
Work is proportional to digits, so it’s O(d).
❓ Frequently Asked Questions
num % 10, which reads the number from right to left.Explore More Python Number Patterns!
Once you understand digit extraction, try sum-of-digits, reverse, and palindrome challenges.
Building a reverse number uses the same core idea as converting digits into a number in base-10: multiply by 10, then add the next digit.
7 people found this page helpful
