A growing reverse-number pattern builds the reverse of an integer one digit at a time — printing the partial reverse after each append — so digits appear from right to left of the original.
Remember
Rule: digit = num % 10
reverse = reverse * 10 + digit
print reverse; num = num // 10
3
32
325
3256
32568 ← start = 86523
In Python peel with num % 10, append into reverse, print(reverse), then num //= 10.
Approach
How to Solve It
Peel digits from the right with %, append them into reverse, print after each append, then shrink num with // 10.
Method
Idea
Best for
while + modulo build
reverse = reverse * 10 + (num % 10), then print
Learning, interviews, digit drills
vs Program 60
60 shrinks num; 61 grows a separate reverse
Comparing digit techniques
Pseudocode
Pseudocode
reverse = 0
while num is not 0:
reverse = reverse * 10 + (num % 10)
print reverse
num = num // 10
Cheat sheet
Goal
Pattern
Loop condition
while num != 0:
Last digit
num % 10
Append digit
reverse = reverse * 10 + (num % 10)
Print partial
print(reverse)
Next digit
num //= 10
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(reverse, end="")
Stays on the same line
Rare here — values would glue together
print(reverse)
Prints the value and ends the line
Each partial reverse
Like Program 60, each iteration needs its own newline — so use bare print(reverse) every step.
Try it
Live Preview
Change the starting number and the growing reverse pattern updates instantly — absolute value, up to 9 digits.
Whole numbers from 1 to 999999999 (negatives use absolute value). Tap a chip or type a value.
Live resultnum = 86523 · 5 lines
3
32
325
3256
32568
Trace
Worked Walkthrough — num = 86523
Trace how each rightmost digit is appended into reverse and printed.
Step
num % 10
reverse after append
1
3
3
2
2
32
3
5
325
4
6
3256
5
8
32568
Final line 32568 is the full reverse of 86523. Five digits → five lines → O(d).
Code
Python Programs
Three complete programs: fixed num = 86523, user-input number, and a compact num = 123 demo. Use View Output to reveal sample results.
Example 1 — Fixed num = 86523
Hard-coded start — append each rightmost digit into reverse and print after every step.
Python
num = 86523
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print(reverse)
num = num // 10
Output
3
32
325
3256
32568
How It Works
1. Peel.num % 10 reads the rightmost digit (3 from 86523).
2. Append.reverse * 10 + digit shifts left and adds the new digit on the right.
3. Shrink.num // 10 drops that digit so the next iteration uses the next one.
Example 2 — User Input Number
Read safely, take absolute value, then run the same append-and-print loop.
Python
try:
num = int(input("Enter a number: "))
except ValueError:
print("Please enter a valid integer.")
raise SystemExit
num = abs(num)
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print(reverse)
num //= 10
Output (when user enters 120)
Enter a number: 120
0
2
21
How It Works
1. Validate. Catch ValueError so bad input does not crash. Python ints grow as needed for large starts.
2. Absolutize.abs(num) keeps digit math positive.
3. Trailing zero demo.120 peels 0 first → lines 0, 2, 21.
Example 3 — Compact num = 123
Three lines only — easy to dry-run on paper before larger demos.
Python
num = 123
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print(reverse)
num //= 10
Output
3
32
321
How It Works
1. Three digits. Peel 3, then 2, then 1 → 3, 32, 321.
2. Trace on paper. Confirm you print after appending — that is what grows each line.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for 86523 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
print before append
Stale reverse values
If you print before updating reverse, the first line is 0 (or empty of the new digit). Append, then print.
forget * 10
Digits overwrite
Without reverse * 10, you only keep the latest digit — never a growing number.
trailing 0
Leading zero line
120 prints 0 first because the rightmost digit is zero — expected, not a bug.
num = 0
Empty output
while num != 0 skips entirely when the start is already 0.
/ vs //
Float leftovers
Use //, not /. True division returns a float and breaks the integer digit loop.
Bad input
int(input()) throws
Wrap with try/except ValueError before the loop so bad input does not crash.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(d)
O(1)
Compact num = 123 (Example 3)
O(d)
O(1)
Each iteration handles one digit, so the loop runs d times for a d-digit start — linear in digit count, constant extra memory.
Remember
Key Takeaways
Append then print:reverse = reverse * 10 + (num % 10), then print(reverse).
Modulo peels right:% 10 reads the next digit; // 10 advances.
vs Program 60: 60 shrinks the original; 61 grows a separate reverse.
Next step: Program 62 fills an n×n spiral matrix.
One line:reverse = reverse * 10 + num % 10, print, then num //= 10.
Frequently Asked Questions
It prints a growing reverse-number pattern like 3, 32, 325, 3256, 32568 when starting from 86523.
It takes the last digit using num % 10 and appends it with reverse = reverse * 10 + digit, then prints reverse.
num = num // 10 removes the last digit so the loop can move to the next digit from the right.
Program 60 prints the shrinking original number. Program 61 builds and prints a growing partial reverse using modulo and multiplication.
Multiplying shifts existing digits left — reverse * 10 + digit appends the new digit on the right.
If the source ends in 0, that digit is extracted first — e.g. 120 gives 0, then 2, then 21.
Apply abs(num) before the loop — see Example 2.
O(d) where d is the number of digits — the loop runs once per digit.
🤔
Did you know?
Each iteration takes the last digit with num % 10, appends it to reverse via reverse = reverse * 10 + digit, prints the partial reverse, then shrinks num with num //= 10 — runtime is O(d) for d digits.