A remove-last-digit pattern prints a number, then drops its rightmost digit each line — 86523 → 8652 → 865 → 86 → 8 — until nothing remains.
Remember
Rule: while num != 0
print num
num = intdiv(num, 10)
86523
8652
865
86
8 ← start = 86523
Unlike nested-loop triangles (Programs 1–59), this uses one while loop and integer division. Program 61 builds a number by reversing digits progressively.
Approach
How to Solve It
Print the current value, then set $num = intdiv($num, 10). Repeat while $num != 0. Prefer intdiv over float division so results stay exact integers.
Method
Idea
Best for
Fixed number
while + intdiv($num, 10)
Labs and demos
CLI input
Same loop; read $num at runtime
Interactive practice
String prefixes
substr($s, 0, $len) shrinking length
Avoiding division
Pseudocode
Pseudocode
num = starting value
while num != 0:
print num and newline
num = integer divide num by 10
Cheat sheet
Goal
Pattern
Set start
$num = 86523;
Loop
while ($num != 0) { ... }
Print line
echo $num . PHP_EOL;
Drop last digit
$num = intdiv($num, 10);
String alternative
echo substr($s, 0, $len) . PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $num
Stays on the same line
Rare here — usually you want a full line
echo $num . PHP_EOL
Prints value and ends the line
Each shortened number (this pattern)
Same idea as C# WriteLine: one number per line. Nested-row patterns use echo for digits and echo PHP_EOL after the row.
Try it
Live Preview
Change the starting number and each digit-removal line updates instantly.
Positive whole numbers up to 9 digits. Tap a chip or type a value — the preview redraws as you go.
Live resultstart = 86523 · lines = 5
86523
8652
865
86
8
Trace
Worked Walkthrough
Trace $num = 86523 — each step prints, then divides by 10.
Iteration
Print
After intdiv(..., 10)
1
86523
8652
2
8652
865
3
865
86
4
86
8
5
8
0 → loop ends
0 is never printed — the while condition fails before that iteration.
Code
PHP Programs
Three complete programs: fixed $num = 86523, CLI input, and a substr variant. Use View Output for sample results.
Example 1 — Fixed $num = 86523
Print the current value, then divide by 10 until the number becomes zero.
1. Convert once. Store the digits as a string so length is known up front.
2. Shorten prefixes.substr($s, 0, $len) keeps the leftmost $len characters.
3. Same look. Visual result matches Examples 1–2 without intdiv.
Edge Cases & Pitfalls
Check these before calling the solution done.
float /
Using $num / 10 without intdiv
In PHP, / can yield floats. Prefer intdiv($num, 10) (or (int) ($num / 10)) for clean integers.
divide first
Dividing before printing
That skips the full starting number. Print first, then intdiv.
num = 0
Starting at zero
The loop never runs — no output. Validate and ask for a non-zero value.
trailing 0
Numbers ending in zero
120 → 12 → 1. Trailing zeros disappear in one step like any last digit.
negatives
Negative input
Without abs, lines may show a minus. Example 2 takes the absolute value first.
single digit
One-digit start
Output is a single line (e.g. 7), then division yields 0 and the loop stops.
Analysis
Time and Space Complexity
Program
Time
Extra space
while + intdiv (Examples 1–2)
O(d)
O(1)
String substr (Example 3)
O(d²) worst case for substr copies
O(d) for the string
For d digits, the while-loop version runs exactly d iterations — linear in the digit count, not in the numeric magnitude alone.
Remember
Key Takeaways
Rule: print, then intdiv($num, 10), until $num is 0.
Prefer intdiv: keeps each step an exact integer without float leftovers.
echo + PHP_EOL: one call per line prints the value and advances.
Next step: Program 61 builds a number by reversing digits step by step.
One line: print the number, divide by 10, repeat until nothing is left.
Frequently Asked Questions
Integer division discards the remainder. So intdiv(86523, 10) becomes 8652, intdiv(8652, 10) becomes 865, and so on.
The loop prints $num then divides. When $num becomes 0, while ($num != 0) is false — 0 is never printed.
Use abs($num) first so each line stays positive without a leading minus.
Yes. Convert to string and print substr($s, 0, $len) — see Example 3.
intdiv(120, 10) becomes 12 immediately — trailing zeros drop like any other last digit.
Yes. Use fgets(STDIN) with is_numeric() and the same while-loop — see Example 2.
Here each line uses echo $num . PHP_EOL — one call prints the value and ends the line. Nested-row patterns often echo digits first, then PHP_EOL after the inner loop.
O(d) where d is the number of digits — one loop iteration per digit.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.
🤔
Did you know?
Integer division by 10 drops the last digit each step — 86523 becomes 8652, then 865, 86, 8. One while loop, O(d) time for d digits. Prefer intdiv($num, 10) in PHP.