A progressive reverse-build peels digits from the right of a number and appends each one to a growing reverse — from 86523 you get 3, 32, 325, 3256, 32568.
Unlike Program 60 (prints the shrinking original), here you print the reverse after every digit is appended. Program 62 continues with a perfect-square spiral grid.
Approach
How to Solve It
Take $num % 10, append it with $reverse * 10 + digit, print $reverse, then intdiv($num, 10). Repeat while $num != 0.
Method
Idea
Best for
Fixed number
while + modulo append + intdiv
Labs and demos
CLI input
Same loop; read $num at runtime
Interactive practice
String accumulator
Append digits as a string
Avoiding integer overflow
Pseudocode
Pseudocode
num = starting value
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print reverse and newline
num = integer divide num by 10
Cheat sheet
Goal
Pattern
Set start
$num = 86523; $reverse = 0;
Last digit
$num % 10
Append
$reverse = $reverse * 10 + ($num % 10);
Print line
echo $reverse . PHP_EOL;
Drop processed digit
$num = intdiv($num, 10);
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $reverse
Stays on the same line
Rare here — usually you want a full line
echo $reverse . PHP_EOL
Prints value and ends the line
Each progressive reverse (this pattern)
Same idea as C# WriteLine: one snapshot of $reverse per line.
Try it
Live Preview
Change the starting number and the progressive reverse 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
3
32
325
3256
32568
Trace
Worked Walkthrough
Trace $num = 86523 — each step appends one digit to $reverse, then shrinks $num.
Iteration
Digit (% 10)
$reverse
After intdiv
1
3
3
8652
2
2
32
865
3
5
325
86
4
6
3256
8
5
8
32568
0 → done
Print happens after each append — that is what makes the reverse progressive instead of only final.
Code
PHP Programs
Three complete programs: fixed $num = 86523, CLI input, and a string accumulator. Use View Output for sample results.
Example 1 — Fixed $num = 86523
Build $reverse digit by digit and print after each append.
3. Same look. For classroom sizes like 86523, output matches Examples 1–2.
Edge Cases & Pitfalls
Check these before calling the solution done.
print last only
Printing only after the loop
That shows one final reverse. Echo $reverse inside the loop for progressive lines.
wrong order
Dividing before appending
That skips the first last digit. Append with % 10, print, 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 builds 0, then 2, then 21 — leading zeros disappear in integer form (first line may print 0).
overflow
Very large integers
On 32-bit PHP, huge reverses can overflow. Use the string accumulator (Example 3) for big inputs.
single digit
One-digit start
Output is a single line equal to the number itself (e.g. 7).
Analysis
Time and Space Complexity
Program
Time
Extra space
while + int reverse (Examples 1–2)
O(d)
O(1)
String accumulator (Example 3)
O(d)
O(d) for the string
For d digits, the loop runs exactly d times — linear in the digit count.
Remember
Key Takeaways
Rule: append $num % 10 to $reverse, print, then intdiv.
Vs Program 60: that shrinks the original; this grows the reverse.
echo + PHP_EOL: one call per line prints the snapshot and advances.
Next step: Program 62 fills a 10×10 grid in a perfect-square spiral.
One line: peel last digits into a growing reverse and print every snapshot.
Frequently Asked Questions
Because 3 is the last digit of 86523. The program takes $num % 10 first and appends it to $reverse.
Each step appends the next last digit to $reverse: 3, then 2 → 32, then 5 → 325, then 6 → 3256.
Once all digits of 86523 are processed, $reverse becomes 32568 and $num becomes 0, ending the loop.
Related — you build the reverse but print it after every step rather than only the final reverse.
Program 60 prints the shrinking original (86523, 8652, …). Program 61 prints the growing reverse (3, 32, 325, …).
Trailing zeros become leading zeros in the reverse, but leading zeros are not shown when printing integers.
Here each line uses echo $reverse . PHP_EOL — one call prints the value and ends the line.
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?
Each step appends the last digit of $num to $reverse with $reverse = $reverse * 10 + ($num % 10), then shrinks $num with intdiv($num, 10). From 86523: 3, 32, 325, 3256, 32568 — O(d) time.