Print First
println(num)
Each iteration prints the current value before shrinking it.

This pattern prints a number, then repeatedly removes the last digit with integer division by 10. Starting from 86523: 86523, 8652, 865, 86, 8. A single while loop handles it — no nested loops needed. This tutorial covers the loop logic, live preview, worked Java examples, edge cases, and O(d) complexity.
println(num)
Each iteration prints the current value before shrinking it.
num / 10
Integer division drops the last digit: 86523 → 8652.
num != 0
Loop runs once per digit until the number becomes zero.
Program 59
Natural break from nested loops — one variable, one while loop.
Any integer
Enter a starting number and see each digit-removal line instantly.
Complexity
One iteration per digit — linear in the number of digits.
A remove-last-digit pattern prints a number line by line, dropping the rightmost digit each time using num / 10.
In Java: while (num != 0), print num, then num = num / 10 until the value reaches zero.
Integer division by 10 is the foundation for digit counting, reversing numbers, and palindrome checks — a natural step after grid patterns in Program 59.
No nested loops — a single while suffices.
Integer division removes the last digit each step.
Program 61 builds the reverse progressively with % 10.
Runs once per digit — very efficient.
In short: while num != 0, print num, then set num = num / 10.
Given starting number num = 86523, print each value as you remove the last digit until the number becomes zero.
// num = 86523 (conceptual output)
// 86523
// 8652
// 865
// 86
// 8 | Item | Type | Description |
|---|---|---|
num | int | Starting positive integer — must be non-zero for the loop to run. |
| Loop condition | boolean | while (num != 0) — stops when division reaches zero. |
| Printed output | text | One line per iteration — full number, then number minus last digit, and so on. |
while num != 0:
print num
num = num / 10 | Approach | Idea | Best for |
|---|---|---|
| While + division | num / 10 each iteration | Classic digit-removal — Example 1 |
| Scanner input | sc.nextInt() for num | User-chosen starting number |
| String substring | s.substring(0, len) | String practice — Example 3 |
| Compound assign | num /= 10 | Shorter equivalent to num = num / 10 |
| Goal | Pattern |
|---|---|
| Set starting number | int num = 86523; |
| Loop condition | while (num != 0) |
| Print current value | System.out.println(num); |
| Remove last digit | num = num / 10; or num /= 10; |
| Handle negatives | num = Math.abs(num); before the loop |
| Program 61 contrast | Program 61 uses % 10 to build reverse progressively |
Three ways to work with digits — this pattern uses division; Program 61 uses modulo.
num = num / 10Removes last digit — used in Examples 1 and 2.
digit = num % 10Extracts last digit — used in Program 61 reverse build.
s.substring(0,len)Same visual output without arithmetic — Example 3.
print then divideAlways print before dividing — otherwise you skip the first value.
Reach for this pattern when teaching while loops, integer division, and digit manipulation without nested loops.
Natural break from nested loops in Program 59 — one variable, one while loop.
Simple loop condition with a clear stopping point when num reaches zero.
Read starting number with Scanner — see Example 2.
Compare with Program 61 (reverse build with % 10), then continue the digit series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in while loops, integer division, and O(d) digit thinking.
Enter a starting number and see each digit-removal line in the browser.
Three complete Java programs — fixed num = 86523, Scanner input, and a string-substring variant. Click View Output to reveal sample console results.
Print five lines from 86523 down to 8 using a while loop.
num = 86523Print the current value, then divide by 10 until the number becomes zero.
public class RemoveLastDigitPattern {
public static void main(String[] args) {
int num = 86523;
while (num != 0) {
System.out.println(num);
num = num / 10;
}
}
} First iteration prints 86523, then num becomes 8652. Each step removes one digit until num is 0 and the loop exits.
Read the starting number with Scanner.
Same while-loop logic; starting number comes from user input.
import java.util.Scanner;
public class RemoveLastDigitInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
while (num != 0) {
System.out.println(num);
num = num / 10;
}
sc.close();
}
} Same digit-removal loop as Example 1; only the source of num changes.
Same output using String.substring instead of division.
Convert to string and print progressively shorter prefixes.
public class RemoveLastDigitString {
public static void main(String[] args) {
int num = 86523;
String s = String.valueOf(num);
for (int len = s.length(); len >= 1; len--) {
System.out.println(s.substring(0, len));
}
}
} substring(0, len) prints prefixes of decreasing length — same visual result without / 10.
int num = 86523; — the value printed on the first line.
while (num != 0) runs once per digit until division reaches zero.
System.out.println(num) outputs the current line before shrinking.
num = num / 10 drops the last digit: 86523 → 8652 → 865 → 86 → 8.
Exactly d lines for d digits — O(d) time, O(1) extra memory.
num = 86523Trace each loop iteration: print, then divide by 10.
| Step | After num / 10 | |
|---|---|---|
| 1 | 86523 | 8652 |
| 2 | 8652 | 865 |
| 3 | 865 | 86 |
| 4 | 86 | 8 |
| 5 | 8 | 0 (loop ends) |
Zero is never printed because the loop condition is checked before the next iteration.
Where integer division by 10 shows up beyond this homework pattern.
Same /10 loop counts how many digits a number has.
Example: loop until num=0 and count iterations.
Combine % 10 and / 10 — see Program 61.
Example: extract last digit with modulo, shrink with division.
Build reversed half while dividing — classic interview prep.
Example: compare original with reversed digits.
Same output with substring — see Example 3.
Example: no arithmetic, just shorter string prefixes.
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: re-prompt when user enters 0.
Pro Tip: always print before dividing — dividing first skips the original value on the first line.
Why this pattern earns a spot in beginner Java courses.
No nested loops — easier than grid patterns in Program 59.
/ 10 and % 10 are core digit-manipulation tools.
Try string variant, negative handling, or continue to Program 61.
Only one integer variable changes — no arrays needed.
Pro Tip: trace 86523 on paper — five lines, five divisions, then stop.
Small habits that keep digit-removal code correct.
Always println(num) first, then num = num / 10.
Keep num as int — floating-point division breaks digit removal.
while (num != 0) stops when the last single digit has been printed and divided.
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 output is missing the first number, you divided before printing.
Mistakes that commonly break digit-removal patterns.
First line shows 8652 instead of 86523 — you skipped the original value.
→ Print num 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 the pattern.
12345, 999, or 120num % 10 before dividingsubstring — Example 3/ 10 removes last digit; % 10 extracts it — pair them in Program 61.num != 0 for interactive programs; zero input prints nothing.Quick Takeaway: print num, then num = num / 10, repeat while num != 0.
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) | O(1) |
| String substring (Example 3) | O(d²) | O(d) for string |
The remove-last-digit pattern is a compact while-loop lesson: print the value, divide by 10, repeat until zero. Master the fixed-num version, then try Scanner input and the string variant in Example 3.
Practice the three examples above, then continue to Program 61 for the progressive reverse-build pattern.
Print before divide — loop while num != 0 — O(d) time for d digits.
num before num = num / 10while (num != 0) as the loop conditionsc.hasNextInt() before using Scanner inputnum inside the loopOne while loop, integer division, O(d) time.
Print then /10
Definitionnum != 0
Codenum / 10
CodeZero not printed
EdgeO(d) time
AnalysisInteger 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.
Move on to the progressive reverse-build number pattern in the Java number-pattern series.
12 people found this page helpful