Extract Digit
num % 10
Modulo gets the last digit: 86523 % 10 → 3.

This pattern builds the reverse of a number step by step, printing after each digit append. Starting from num = 86523: 3, 32, 325, 3256, 32568. Use num % 10 to extract digits and num / 10 to shrink the source. This tutorial covers the loop logic, live preview, worked Java examples, edge cases, and O(d) complexity.
num % 10
Modulo gets the last digit: 86523 % 10 → 3.
reverse * 10 + digit
Shift reverse left and add the digit: 0 → 3 → 32 → 325.
println(reverse)
Each iteration prints the growing reverse value.
% 10 + / 10
Natural next step — Program 60 printed num; this builds reverse.
Any integer
Enter a starting number and see each progressive reverse line instantly.
Complexity
One iteration per digit — linear in the number of digits.
A progressive reverse-build pattern appends the last digit of num to a running reverse value and prints it each step.
In Java: while (num != 0), update reverse = reverse * 10 + (num % 10), print reverse, then num = num / 10.
Pairing % 10 and / 10 is the core technique for reversing numbers, counting digits, and checking palindromes — the natural follow-up to Program 60.
num % 10 extracts the last digit each step.
reverse * 10 + digit appends to the right.
Program 60 printed num; this prints growing reverse.
Runs once per digit — very efficient.
In short: while num != 0, append num % 10 to reverse, print reverse, then num = num / 10.
Given num = 86523, build and print reverse after each digit is appended: 3, 32, 325, 3256, 32568.
int num = 86523;
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + (num % 10);
System.out.println(reverse);
num = num / 10;
} | Item | Type | Description |
|---|---|---|
num | int | Starting positive integer — loop runs while num != 0. |
reverse | int | Running reverse built with reverse * 10 + (num % 10). |
| Printed output | text | One line per iteration — growing reverse: 3, 32, 325, 3256, 32568. |
reverse = 0
while num != 0:
reverse = reverse * 10 + (num % 10)
print reverse
num = num / 10 | Approach | Idea | Best for |
|---|---|---|
| While + modulo | reverse * 10 + num % 10 | Classic reverse build — Example 1 |
| Scanner input | sc.nextInt() for num | User-chosen starting number |
| long accumulator | long reverse for large inputs | Overflow-safe variant — Example 3 |
| Program 60 contrast | Print num then /10 | Removes digits instead of building reverse |
| Goal | Pattern |
|---|---|
| Init accumulator | int reverse = 0; |
| Loop condition | while (num != 0) |
| Extract last digit | num % 10 |
| Append to reverse | reverse = reverse * 10 + (num % 10); |
| Print progressive line | System.out.println(reverse); |
| Shrink num | num = num / 10; |
Three teaching angles — progressive reverse build (this program), digit removal (Program 60), and overflow-safe long.
reverse = reverse * 10 + num % 10Core pattern for Examples 1 and 2.
println(num); num /= 10Prints shrinking num instead of growing reverse.
long reverse = 0;Safer for very large inputs — Example 3.
build, print, then /10Update reverse and print before dividing num.
Use progressive reverse-build when teaching modulo, digit extraction, and partial reverse snapshots in one while loop.
Natural next step once students can divide by 10 — now combine with % 10.
Print intermediate reverses before the classic single-line reverse program.
Pair with Scanner — see Example 2.
Continue to Program 62 (Perfect Square Spiral) after mastering digit loops.
Console teaching pattern — builds algorithmic thinking, not screen layouts.
Key benefit: one loop that connects modulo, multiply-by-10, and progressive output.
Enter a starting number and see each progressive reverse-build line.
Three complete Java programs — fixed num = 86523, Scanner input, and a long overflow-safe variant. Click View Output to reveal sample console results.
Print five progressive reverse lines from 86523: 3, 32, 325, 3256, 32568.
num = 86523Build reverse digit by digit and print after each append.
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;
}
}
} First iteration takes digit 3, sets reverse to 3, then num becomes 8652. Each step appends the next last digit until num is 0.
Read the starting number with Scanner.
Same reverse-build loop; starting number comes from user input.
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();
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + (num % 10);
System.out.println(reverse);
num = num / 10;
}
sc.close();
}
} Same progressive reverse build as Example 1; only the source of num changes.
Use long for reverse when inputs may be large.
long AccumulatorIdentical logic with long reverse to reduce overflow risk on big numbers.
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;
}
}
} long widens the range for reverse as it grows — same output for classroom-sized inputs like 86523.
int reverse = 0; and int num = 86523; before the loop.
num % 10 reads the last digit (3, then 2, then 5, …).
reverse = reverse * 10 + (num % 10); grows the reverse on the right.
System.out.println(reverse); shows 3, 32, 325, 3256, 32568.
num = num / 10; removes the processed digit and moves to the next.
Exactly d lines for d digits — O(d) time, O(1) extra space.
num = 86523Trace each loop iteration: extract digit, update reverse, print, then divide.
| Step | num | digit | reverse printed | num after /10 |
|---|---|---|---|---|
| 1 | 86523 | 3 | 3 | 8652 |
| 2 | 8652 | 2 | 32 | 865 |
| 3 | 865 | 5 | 325 | 86 |
| 4 | 86 | 6 | 3256 | 8 |
| 5 | 8 | 8 | 32568 | 0 (loop ends) |
The loop stops when num becomes zero after the fifth division.
Where progressive reverse-build and digit-manipulation loops show up in Java courses.
Same loop structure prints only the final reverse instead of each step.
Example: move println outside the loop to print only the final reverse.
Combine % 10 and / 10 to build a reversed half for comparison.
Example: compare digits from both ends using modulo and division.
Same /10 loop counts how many digits a number has.
Example: increment a counter each iteration until num is 0.
Program 60 printed shrinking num; this prints growing reverse.
Example: compare both outputs side by side from the same starting value.
Linear in digit count — much faster than grid patterns for large numbers.
Example: 86523 has 5 digits → 5 loop iterations.
Pair with Scanner and reject zero or negative input if required.
Example: use sc.hasNextInt() before reading input.
Pro Tip: always update and print reverse before dividing num — dividing first skips the current digit.
Why progressive reverse-build belongs in beginner Java courses.
No nested loops — natural follow-up to Program 60’s digit-removal loop.
num % 10 and reverse * 10 + digit are core reverse tools.
Try Scanner input, long variant, or continue to Program 62.
Only one integer variable changes — no arrays needed.
Pro Tip: trace 86523 on paper — five lines, five digit appends, then stop.
Small habits that keep reverse-build code correct.
Always update and println(reverse) first, then num = num / 10.
Keep num as int — floating-point division breaks digit removal.
while (num != 0) stops when the last digit has been appended, printed, and divided away.
If num starts at 0, the loop never runs — validate or show a message.
Compound assignment is equivalent to num = num / 10.
Pro Tip: if the first line is wrong, you divided before appending the digit to reverse.
Mistakes that commonly break progressive reverse-build patterns.
First line shows 32 instead of 3 — you divided before appending the current digit.
→ Update reverse and print first, then divide.
Forgetting num = num / 10 leaves num unchanged forever.
→ Always update num inside the loop body.
Using double can introduce precision issues on very large values.
→ Use int or long for clean digit removal.
Letters or empty input leave num uninitialized.
→ Call sc.hasNextInt() before nextInt().
while (num != 0) never executes — no output at all.
→ Validate input or show a message when num is 0.
Check these inputs before calling the solution done.
Loop never runs — print nothing or show a message.
Output is one line: 8 — then num becomes 0.
Prints 120 then 12 then 1 — zero drops immediately.
Use Math.abs(num) first for clean positive output.
Call sc.hasNextInt() before reading num.
Use long if values exceed Integer.MAX_VALUE.
Try these variations to lock in progressive reverse-build.
12345, 999, or 120reverse once without intermediate lineslong reverse — Example 3% 10 extracts the last digit; / 10 removes it — pair them in every reverse-build loop.num != 0 for interactive programs; zero input prints nothing.num) — this pattern prints growing reverse.Quick Takeaway: append num % 10 to reverse, print reverse, then num = num / 10, repeat while num != 0.
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) | O(1) |
| long variant (Example 3) | O(d) | O(1) |
The progressive reverse-build pattern is a compact while-loop lesson: extract the last digit, append to reverse, print, divide by 10, repeat until zero. Master the fixed-num version, then try Scanner input and the long variant in Example 3.
Practice the three examples above, then continue to Program 62 for the Perfect Square Spiral pattern.
Build then print — loop while num != 0 — O(d) time for d digits.
reverse before num = num / 10while (num != 0) as the loop conditionsc.hasNextInt() before using Scanner inputreversenum inside the loopOne while loop, modulo append, O(d) time.
reverse * 10 + digit
Definitionnum != 0
Codenum % 10
Coded lines for d digits
EdgeO(d) time
AnalysisEach 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.
Move on to the Perfect Square Spiral pattern in the Java number-pattern series.
12 people found this page helpful