A remove-last-digit pattern prints an integer, then repeatedly strips the rightmost digit with integer division by 10 until the value becomes zero.
Remember
Rule: while num != 0 → print num → num = num // 10
86523
8652
865
86
8 ← start = 86523
In Python use while num != 0, print(num), then num = num // 10 — floor division drops the last digit.
Approach
How to Solve It
Use a while loop: print the current value, then divide by 10 with //. Stop when num reaches 0.
Method
Idea
Best for
while + divide by 10
Print, then num //= 10 until zero
Learning, interviews, digit drills
Abs for negatives
num = abs(num) before the loop
User input that may be negative
Pseudocode
Pseudocode
while num is not 0:
print num
num = num // 10
Cheat sheet
Goal
Pattern
Loop condition
while num != 0:
Print current
print(num)
Drop last digit
num = num // 10 or num //= 10
Handle negatives
num = abs(num)
Line count
Equals the digit count of the starting number
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(num, end="")
Stays on the same line
Rare here — values would glue together
print(num)
Prints the value and ends the line
Each step of the pattern
Unlike nested triangle patterns, each iteration needs its own newline — so use bare print(num) every step.
Try it
Live Preview
Change the starting number and the digit-removal 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
86523
8652
865
86
8
Trace
Worked Walkthrough — num = 86523
Trace each print-then-divide step until the loop stops.
Step
Prints
After // 10
1
86523
8652
2
8652
865
3
865
86
4
86
8
5
8
0 (loop ends)
Five digits → five lines → O(d) time. Floor division truncates toward negative infinity for negatives — use abs first for unsigned digit stripping.
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 — print, then divide by 10 until zero.
Python
num = 86523
while num != 0:
print(num)
num = num // 10
Output
86523
8652
865
86
8
How It Works
1. Print first. Show the current num before changing it.
2. Divide by 10. Floor division drops the last digit — 86523 // 10 is 8652.
3. Stop at zero. When num becomes 0, while num != 0 ends.
Example 2 — User Input Number
Read num safely, take absolute value, then run the same loop.
Python
try:
num = int(input("Enter a number: "))
except ValueError:
print("Please enter a valid integer.")
raise SystemExit
num = abs(num)
while num != 0:
print(num)
num //= 10
Output (when user enters 120)
Enter a number: 120
120
12
1
How It Works
1. Prompt and validate. Catch ValueError so bad input does not crash the script.
2. Absolutize.abs(num) keeps the digit-strip logic positive.
3. Same core. Print-then-divide matches Example 1 — only the source of num changes.
Example 3 — Compact num = 123
Three lines only — easy to dry-run on paper before larger demos.
Python
num = 123
while num != 0:
print(num)
num //= 10
Output
123
12
1
How It Works
1. Three digits.123 → 12 → 1 → 0 — three prints, then stop.
2. Trace on paper. Confirm you print before dividing; reverse that order and you miss the first 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.
divide first
Missing the first line
If you divide before printing, the starting number never appears. Always print, then num //= 10.
num = 0
Empty output
while num != 0 skips entirely when the start is already 0 — no lines are printed.
trailing 0
Zeros disappear next step
120 prints 120, then 12, then 1 — the trailing zero is not a separate line after division.
negatives
Sign without abs
Without abs, negative starts print with a minus and still shrink toward zero — use abs when you want unsigned digit stripping.
end=""
All on one line
If you use print(num, end="") instead of print(num), every value glues together on one line.
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 removes one digit, so the loop runs d times for a d-digit start — linear in digit count, constant extra memory.
Remember
Key Takeaways
Print then divide: show num, then num //= 10.
while until zero: digit count is unknown ahead of time — while num != 0 fits naturally.
Integer division:// 10 truncates — that is how the last digit disappears.
Next step: Program 61 builds a growing reverse number with modulo.
One line:while num != 0: print(num); num //= 10
Frequently Asked Questions
It prints the starting number on each line while removing the last digit each step. For 86523, output is 86523, 8652, 865, 86, 8.
Integer division by 10 discards the remainder: 86523 // 10 becomes 8652, then 865, then 86, then 8.
The number of steps equals the digit count — unknown until you read the input. while num != 0 keeps going until all digits are stripped.
Program 59 uses nested loops on a grid. Program 60 uses one while loop and integer division on a single number.
Program 60 shrinks the original number by dividing by 10. Program 61 builds a growing reverse number using modulo and division.
A trailing 0 is removed on the next step — 120 becomes 12, then 1.
Apply abs(num) before the loop — see Example 2.
O(d) where d is the number of digits — each iteration removes exactly one digit.
🤔
Did you know?
Each iteration prints the current number, then num = num // 10 drops the last digit using integer division — runtime is O(d) for d digits.