A remove-last-digit pattern prints a number, then repeatedly drops the rightmost digit with integer division by 10 until nothing remains.
Remember
Rule: while num != 0
print num
num = num / 10
86523
8652
865
86
8 ← start = 86523
No nested loops — one while loop runs once per digit. Pair with Program 61 (% 10) for the reverse-build twin.
Approach
How to Solve It
Print the current value, then divide by 10. Repeat until the value becomes zero.
Method
Idea
Best for
While + /10
Print then num /= 10
Learning integer division
Scanner input
Same loop; read num at runtime
Interactive practice
String prefix
substring(0, len) shrinking
Avoiding math
Pseudocode
Pseudocode
num = starting number
while num != 0:
print num
num = num / 10
Cheat sheet
Goal
Pattern
Set start
int num = 86523;
Loop
while (num != 0)
Print line
System.out.println(num);
Drop last digit
num = num / 10;
Lines printed
Equal to digit count d
String alternative
s.substring(0, len) for len = d..1
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Not needed here — each value is a full line
System.out.println
Ends the line
Each current num (or string prefix)
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 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 resultnum = 86523 · lines = 5
86523
8652
865
86
8
Trace
Worked Walkthrough — num = 123
Trace print-then-divide for three digits.
Step
num before
Printed
After /10
1
123
123
12
2
12
12
1
3
1
1
0 → loop ends
When num becomes 0, the condition fails — 0 is never printed.
Code
Java Programs
Three complete programs: fixed 86523, Scanner input, and a string-substring variant. Use View Output to reveal sample results.
Example 1 — Fixed num = 86523
Print the current value, then divide by 10 until the number becomes zero.
Java
public class RemoveLastDigitPattern {
public static void main(String[] args) {
int num = 86523;
while (num != 0) {
System.out.println(num);
num = num / 10;
}
}
}
Output
86523
8652
865
86
8
How It Works
1. Print first. Each iteration shows the current num before shrinking it.
2. Divide by 10. Integer division drops the last digit: 86523 → 8652 → … → 8.
3. Stop at zero. After printing 8, num becomes 0 and the loop exits.
Example 2 — Scanner Input
Same while loop; starting number comes from the user.
Java
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();
if (num < 0) num = Math.abs(num);
if (num == 0) {
System.out.println(0);
return;
}
while (num != 0) {
System.out.println(num);
num = num / 10;
}
sc.close();
}
}
Output (when user enters 86523)
Enter a number: 86523
86523
8652
865
86
8
How It Works
1. Read and normalize. Absolute value avoids a minus on every line; special-case 0 so something still prints.
2. Same loop. Only the source of num changes — print then / 10.
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 — String Substring
Same visual result using shrinking prefixes — no division.
Java
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));
}
}
}
Output
86523
8652
865
86
8
How It Works
1. Convert once.String.valueOf(num) gives the digit characters.
2. Shrink the prefix.substring(0, len) for len = d..1 drops the rightmost character each row.
3. Same shape. Output matches Examples 1 and 2 character for character.
Edge Cases & Pitfalls
Check these before calling the solution done.
Divide first
Missing first line
If you divide before printing, the full number never appears. Always print, then / 10.
float / double
Broken digits
Use int or long. Floating-point division does not drop digits cleanly.
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, then becomes 0.
Trailing zero
Zeros vanish
120 → 12 → 1. The trailing zero is removed like any last digit.
Negative
Use Math.abs
Without Math.abs, every line may show a leading minus. Normalize first.
Analysis
Time and Space Complexity
Program
Time
Extra space
While + /10 (Examples 1–2)
O(d)
O(1)
String substring (Example 3)
O(d²)*
O(d) for the string
d = digit count. *Each substring may copy up to d characters, so total work can be quadratic; the while-loop version stays linear.
Remember
Key Takeaways
Rule: print num, then num = num / 10, until zero.
Lines: exactly d lines for a d-digit number.
Pair:/ 10 removes; % 10 extracts — see Program 61.
Complexity:O(d) time with the while-loop approach.
One line: print the number, chop the last digit with / 10, repeat.
Frequently Asked Questions
Integer division discards the remainder. So 86523/10 becomes 8652, 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.
Yes, but use Math.abs(num) first so output has no leading minus on every line.
Yes. Convert to String and print substring(0, len) — see Example 3.
120/10 becomes 12 immediately — trailing zeros drop like any other last digit.
Yes. Use Scanner.nextInt() and the same while 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?
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.