Python Number Pattern (Progressive Reverse)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Peel digits from the right with %, append them into reverse, print after each append, then shrink num with // 10.

MethodIdeaBest for
while + modulo buildreverse = reverse * 10 + (num % 10), then printLearning, interviews, digit drills
vs Program 6060 shrinks num; 61 grows a separate reverseComparing digit techniques

Pseudocode

Pseudocode
reverse = 0
while num is not 0:
    reverse = reverse * 10 + (num % 10)
    print reverse
    num = num // 10

Cheat sheet

GoalPattern
Loop conditionwhile num != 0:
Last digitnum % 10
Append digitreverse = reverse * 10 + (num % 10)
Print partialprint(reverse)
Next digitnum //= 10

Printing Numbers vs Starting a New Line

APIEffectUse for
print(reverse, end="")Stays on the same lineRare here — values would glue together
print(reverse)Prints the value and ends the lineEach partial reverse

Like Program 60, each iteration needs its own newline — so use bare print(reverse) every step.

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 result num = 86523 · 5 lines
3
32
325
3256
32568

Worked Walkthrough — num = 86523

Trace how each rightmost digit is appended into reverse and printed.

Stepnum % 10reverse after append
133
2232
35325
463256
5832568

Final line 32568 is the full reverse of 86523. Five digits → five lines → O(d).

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

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

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

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Perfect Square Spiral

Continue with a 2D spiral that fills an n×n grid with consecutive numbers.

Program 62 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful