Java Reverse Number Triangle Pattern (Growing)

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

What Is This Pattern?

A growing reverse triangle starts with 1 and grows by one digit on the left each row — digits on row i run from i down to 1.

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

1
21
321
4321
54321     ← n = 5

Unlike Program 5 (1, 12, 123), here the inner loop counts down — same growth, reversed digit order.

How to Solve It

Grow the row index upward; on each row, print from that index down to 1.

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

Pseudocode

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

Cheat sheet

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

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 growing reverse triangle 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
1
21
321
4321
54321

Worked Walkthrough — n = 3

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

iInner jPrinted row
111
22 121
33 2 1321

Each step raises the start of the countdown by 1, so one more digit appears on the left.

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 grows; each row prints i..1.

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

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

How It Works

1. Outer loop. i runs from 1 to n — each row is one digit longer.

2. Inner loop. For each i, print j from i down to 1 — first row is 1, then 21, 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 GrowingReverseTriangleInput {
    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 = 1; i <= n; i++) {
            for (int j = i; j >= 1; 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 up, inner i..1.

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 GrowingReverseTriangleSpaced {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = i; j >= 1; 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.

j ascending

Wrong digit order

Using j = 1..i prints 1, 12, 123 — that is Program 5, not this pattern.

println inside

Vertical digits

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

j from n

Wrong shape

Starting at j = n every row builds a different pattern — use j = i so row length equals i.

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 = 1..n; print j = i..1 on each row.
  • Growth: one new digit appears on the left each row.
  • Vs Program 5: same outer loop; flip the inner direction to reverse digits.
  • Complexity: O(n²) for n rows.

One line: grow the row length upward, and count down from the row index to 1 on each line.

Frequently Asked Questions

Each row must print digits from i down to 1. Starting at j = i and using j-- gives 321 on row 3 instead of 123.
The outer loop runs i from 1 to n. For each row i, the inner loop runs j from i down to 1 and prints j, then println ends the row.
Program 5 prints 1, 12, 123 (inner loop j = 1..i ascending). Program 7 prints 1, 21, 321 (same outer loop, inner loop j = i..1 descending).
Program 6 uses a descending outer loop and prints 5, 45, 345 (inner j = i..n). Program 7 uses an ascending outer loop and counts j down on each row (1, 21, 321).
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 grows row index i; inner loop prints j from i down to 1 — 1, 21, 321, … O(n²) for n rows.

Next: Reverse Growing Number Pattern

Continue with the reverse growing number pattern in the Java number-pattern series.

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