Java Number Pattern (Reverse Growing)

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

What Is This Pattern?

A reverse growing pattern starts with a single high digit and grows by one digit on the right each row — always counting down from n toward the current stop value i.

Remember
Rule: for i = n down to 1
        print j from n down to i

5
54
543
5432
54321     ← n = 5

Twin of Program 7 (1, 21, 321): here digits always start at n and grow toward the full countdown.

How to Solve It

Count the stop value down from n; on each row, print from n down to that stop.

MethodIdeaBest for
Fixed nOuter i = n..1; inner j = n..iLabs and demos
Scanner inputSame loops; read n at runtimeInteractive practice
Spaced digitsprint(j + " ")Easier reading

Pseudocode

Pseudocode
for i from n down to 1:
    for j from n down to i:
        print j
    new line

Cheat sheet

GoalPattern
Set sizeint n = 5;
Outer loopfor (int i = n; i >= 1; i--)
Inner loopfor (int j = n; j >= i; j--)
Print digitSystem.out.print(j);
End rowSystem.out.println();
Digits on rown - i + 1

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit in the row
System.out.printlnEnds the lineAfter the inner loop finishes

Build the row with print, then break once.

Live Preview

Change n and the reverse growing pattern updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · digits = 15
5
54
543
5432
54321

Worked Walkthrough — n = 3

Trace each outer value of i and the digits printed on that row.

iInner jPrinted row
333
23 232
13 2 1321

Each step lowers the stop value i, so one more digit appears on the right.

Java Programs

Three complete programs: fixed n = 5, Scanner input, and spaced digits. Use View Output to reveal sample results.

Example 1 — Fixed n = 5

Hard-coded height — outer loop counts down; each row prints n..i.

Java
public class ReverseGrowingPattern {
    public static void main(String[] args) {
        int n = 5;

        for (int i = n; i >= 1; i--) {
            for (int j = n; j >= i; j--) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer loop. i starts at n and counts down to 1 — the stop value for the inner loop.

2. Inner loop. Print j from n down to i — first row is 5, then 54, and so on.

3. Newline. println() after the inner loop starts the next row.

Example 2 — Scanner Input

Read n at runtime. Same nested-loop core.

Java
import java.util.Scanner;

public class ReverseGrowingPatternInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int n = sc.nextInt();
        if (n < 1) return;

        for (int i = n; i >= 1; i--) {
            for (int j = n; j >= i; j--) {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and guard. Read n; exit early if it is less than 1.

2. Same loops. Only the source of n changes — outer n..1, inner n..i.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int n = sc.nextInt();
if (n < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — Spaced Digits

Same loops; print a space after each digit for easier reading.

Java
public class ReverseGrowingPatternSpaced {
    public static void main(String[] args) {
        int n = 5;

        for (int i = n; i >= 1; i--) {
            for (int j = n; j >= i; j--) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Same shape. Outer and inner bounds match Examples 1 and 2.

2. Formatting only. j + " " separates digits — useful when numbers grow past single digits.

3. Trailing space. Each row may end with a space; that is normal for this simple style.

Edge Cases & Pitfalls

Check these before calling the solution done.

Outer ascending

Wrong row order

Counting i up from 1 with j = n..i shrinks the pattern (Program 4 style), not grows it.

j = i..1

Wrong digits

That is Program 7 (1, 21, 321). Here always start at n.

println inside

Vertical digits

Calling println inside the inner loop prints one digit per line. Keep it after the inner loop.

n = 1

Single digit

Output is just 1 on one line.

n ≤ 0

Empty output

The outer loop never runs. Guard interactive input with n >= 1.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

Total digits printed: 1 + 2 + … + n = n(n+1)/2 → still O(n²).

Key Takeaways

  • Rule: outer i = n..1; print j = n..i on each row.
  • Growth: one new digit appears on the right each row.
  • Vs Program 7: start every row at n, not at i.
  • Complexity: O(n²) for n rows.

One line: lower the stop value from n toward 1, and always count down from n to that stop.

Frequently Asked Questions

When i = n, the inner loop runs j from n down to i (n), so only one digit prints. Each later row lowers i, so more digits appear: 54, 543, and so on.
The outer loop runs i from n down to 1. For each i, the inner loop runs j from n down to i and prints j, then println ends the row.
Program 4 shrinks each row (54321, 5432, 543). Program 8 grows each row from one digit up to n digits (5, 54, 543) using the same inner range j = n..i.
Program 7 uses outer i = 1..n and inner j = i..1 (1, 21, 321). Program 8 uses outer i = n..1 and inner j = n..i (5, 54, 543).
Yes. Use System.out.print(j + " ") inside the inner loop — see Example 3.
O(n²) for n rows. Total printed digits are 1+2+…+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Outer loop counts i down from n; inner loop prints j from n down to i — 5, 54, 543, … O(n²) for n rows.

Next: Repeated Number Triangle Pattern

Continue with the repeated number triangle in the Java number-pattern series.

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