Java Number Pattern (Remove Last Digit)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Print the current value, then divide by 10. Repeat until the value becomes zero.

MethodIdeaBest for
While + /10Print then num /= 10Learning integer division
Scanner inputSame loop; read num at runtimeInteractive practice
String prefixsubstring(0, len) shrinkingAvoiding math

Pseudocode

Pseudocode
num = starting number
while num != 0:
    print num
    num = num / 10

Cheat sheet

GoalPattern
Set startint num = 86523;
Loopwhile (num != 0)
Print lineSystem.out.println(num);
Drop last digitnum = num / 10;
Lines printedEqual to digit count d
String alternatives.substring(0, len) for len = d..1

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineNot needed here — each value is a full line
System.out.printlnEnds the lineEach current num (or string prefix)

Same idea as C# WriteLine: every iteration is one complete line, so use println only.

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 result num = 86523 · lines = 5
86523
8652
865
86
8

Worked Walkthrough — num = 123

Trace print-then-divide for three digits.

Stepnum beforePrintedAfter /10
112312312
212121
3110 → loop ends

When num becomes 0, the condition fails — 0 is never printed.

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;
        }
    }
}

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();
    }
}

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));
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Progressive Reverse-Build Number Pattern

Continue with the reverse-build pattern using modulo in the Java number-pattern series.

Program 61 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful