Java Number Pattern (Progressive Reverse)

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

What Is This Pattern?

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.

How to Solve It

Extract the last digit, append it to reverse, print, then shrink num.

MethodIdeaBest for
While + % /Append digit, print, divideLearning reverse build
Scanner inputSame loop; read num at runtimeInteractive practice
long reverseWider type for large valuesOverflow-safe labs

Pseudocode

Pseudocode
reverse = 0
while num != 0:
    reverse = reverse * 10 + (num % 10)
    print reverse
    num = num / 10

Cheat sheet

GoalPattern
Initint reverse = 0;
Loopwhile (num != 0)
Last digitnum % 10
Appendreverse = reverse * 10 + (num % 10);
PrintSystem.out.println(reverse);
Shrinknum = num / 10;

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineNot needed — each reverse is a full line
System.out.printlnEnds the lineEach growing reverse value

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

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 result num = 86523 · lines = 5
3
32
325
3256
32568

Worked Walkthrough — num = 123

Trace append → print → divide for three digits.

StepnumDigit %10reverse printed
112333
212232
311321

After step 3, num becomes 0 and the loop ends — final reverse is 321.

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

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

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

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

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(d)O(1)

d = digit count. One iteration per digit; only a few integers of extra memory.

Key Takeaways

  • Rule: reverse = reverse * 10 + (num % 10), print, then num / 10.
  • Vs Program 60: that shrinks num; this grows reverse.
  • Pair: % 10 extracts; / 10 removes — core reverse tools.
  • Complexity: O(d) time for d digits.

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.

Next: Perfect Square Spiral (10×10)

Continue with the perfect square spiral pattern in the Java number-pattern series.

Program 62 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