A progressive reverse-build pattern grows the reverse of a number digit by digit, printing after each append — snapshots of the reverse as it forms.
Remember
Rule: while num != 0
reverse = reverse * 10 + (num % 10)
print reverse
num = num / 10
3
32
325
3256
32568 ← from 86523
Twin of Program 60: that printed shrinking num; this prints growing reverse using % 10 + / 10.
Approach
How to Solve It
Extract the last digit, append it to reverse, print, then shrink num.
Method
Idea
Best for
While + % /
Append digit, print, divide
Learning reverse build
Scanner input
Same loop; read num at runtime
Interactive practice
long reverse
Wider type for large values
Overflow-safe labs
Pseudocode
Pseudocode
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print reverse
num = num / 10
Cheat sheet
Goal
Pattern
Init
int reverse = 0;
Loop
while (num != 0)
Last digit
num % 10
Append
reverse = reverse * 10 + (num % 10);
Print
System.out.println(reverse);
Shrink
num = num / 10;
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Not needed — each reverse is a full line
System.out.println
Ends the line
Each growing reverse value
Same idea as C# WriteLine: every iteration is one complete line, so use println only.
Try it
Live Preview
Change the starting number and each progressive reverse line updates instantly.
Positive whole numbers up to 9 digits. Tap a chip or type a value — the preview redraws as you go.
Live resultnum = 86523 · lines = 5
3
32
325
3256
32568
Trace
Worked Walkthrough — num = 123
Trace append → print → divide for three digits.
Step
num
Digit %10
reverse printed
1
123
3
3
2
12
2
32
3
1
1
321
After step 3, num becomes 0 and the loop ends — final reverse is 321.
Code
Java Programs
Three complete programs: fixed 86523, Scanner input, and a long reverse variant. Use View Output to reveal sample results.
Example 1 — Fixed num = 86523
Build reverse digit by digit and print after each append.
Java
public class ProgressiveReverseBuildPattern {
public static void main(String[] args) {
int num = 86523;
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + (num % 10);
System.out.println(reverse);
num = num / 10;
}
}
}
Output
3
32
325
3256
32568
How It Works
1. Extract.num % 10 reads the last digit (3, then 2, then 5, …).
2. Append and print.reverse * 10 + digit grows the reverse on the right; println shows each snapshot.
3. Shrink.num / 10 drops the used digit so the next iteration moves left.
Example 2 — Scanner Input
Same reverse-build loop; starting number comes from the user.
Java
import java.util.Scanner;
public class ProgressiveReverseBuildInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num < 0) num = Math.abs(num);
if (num == 0) {
System.out.println(0);
return;
}
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + (num % 10);
System.out.println(reverse);
num = num / 10;
}
sc.close();
}
}
Output (when user enters 86523)
Enter a number: 86523
3
32
325
3256
32568
How It Works
1. Read and normalize. Absolute value avoids a minus; special-case 0 so something still prints.
2. Same loop. Only the source of num changes — append, print, divide.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter an integer.");
return;
}
int num = Math.abs(sc.nextInt());
Example 3 — long Reverse
Identical logic with long reverse to reduce overflow risk on big numbers.
Java
public class ProgressiveReverseBuildLong {
public static void main(String[] args) {
long num = 86523L;
long reverse = 0L;
while (num != 0) {
reverse = reverse * 10L + (num % 10L);
System.out.println(reverse);
num = num / 10L;
}
}
}
Output
3
32
325
3256
32568
How It Works
1. Wider type.long holds larger intermediate reverses as digits accumulate.
2. Same formula.reverse * 10 + (num % 10) is unchanged — only the type widens.
3. Same shape. For classroom-sized inputs like 86523, output matches Examples 1 and 2.
Edge Cases & Pitfalls
Check these before calling the solution done.
Divide first
Skipped digit
Always update and print reverse before num / 10. Dividing first loses the current digit.
No divide
Infinite loop
Forgetting num = num / 10 leaves num unchanged forever.
Trailing zero
Leading zero lost
120 prints 0, then 2, then 21 — integer print hides a leading zero in the middle.
num = 0
Empty loop
while (0 != 0) never runs. Special-case zero if you want a single 0 line.
Single digit
One line
Input 8 prints 8 once — reverse equals the digit.
Overflow
Use long
Very large inputs can overflow int reverse — prefer long (Example 3).
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(d)
O(1)
d = digit count. One iteration per digit; only a few integers of extra memory.
Remember
Key Takeaways
Rule:reverse = reverse * 10 + (num % 10), print, then num / 10.
Vs Program 60: that shrinks num; this grows reverse.
One line: peel digits from the right and glue them onto a growing reverse, printing each step.
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.
Trailing zeros become leading zeros in the reverse, but leading zeros are not shown in integer printing (e.g. 120 → 0, 2, 21).
Yes. Use Scanner.nextInt() and the same loop — see Example 2.
O(d) where d is the number of digits — one loop iteration per digit.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Each step appends the last digit of num to reverse with reverse = reverse * 10 + (num % 10), then shrinks num with / 10. From 86523: 3, 32, 325, 3256, 32568 — O(d) time.