Java Number Triangle Pattern (Reverse Descending)

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

What Is This Pattern?

A reverse descending number triangle shrinks each row while printing digits from i down to 1 — so the top row is longest and the bottom row is a single 1.

Remember
Rule: for i from rows down to 1,
      print j from i down to 1

54321
4321
321
21
1     ← rows = 5

Unlike Program 2 (left-shifted i..rows), here both loops count downward and each row ends at 1.

How to Solve It

Descend i from rows to 1; for each row, print j from i down to 1.

MethodIdeaBest for
Descending nested loopsOuter i = rows..1; inner j = i..1Learning, interviews, exams
Spaced digitsSame loops; print each digit with a trailing spaceWhen wide rows need gaps

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from i down to 1:
        print j
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = rows; i >= 1; i--)
Print digitsfor (int j = i; j >= 1; j--) System.out.print(j);
End the rowSystem.out.println();
Spaced digitsSystem.out.print(j + " ");
Ascending variantKeep outer; change inner to j = 1..i

Printing Numbers vs Starting a New Line

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

Print digits without a newline, then end the row once.

Live Preview

Change the row count and the reverse descending triangle updates instantly.

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

Live result rows = 5 · 15 digits
54321
4321
321
21
1

Worked Walkthrough — rows = 5

Trace i, the inner range j = i..1, and the printed row.

iInner jDigitsPrinted row
55..1554321
44..144321
33..13321
22..1221
11..111

Total digits = 5 + 4 + 3 + 2 + 1 = 15 — the triangular number n(n+1)/2.

Java Programs

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

Example 1 — Fixed rows = 5

Hard-coded size — both loops count downward.

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

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

            System.out.println();
        }
    }
}

How It Works

1. Outer loop shrinks. i runs from 5 down to 1 — each step shortens the row.

2. Inner loop descends. Print j from i down to 1 with print.

3. Break the row. Call println once after the inner loop.

When i = 5: 54321. When i = 1: a single 1.

Example 2 — Rows Input

Read rows at runtime. Same nested descending loops.

Java
import java.util.Scanner;

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

        for (int i = rows; i >= 1; 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 rows; exit early if it is less than 1.

2. Same core. Only the source of rows changes from a literal to user input.

3. Safer input tip. Prefer:

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

Example 3 — Spaced Digits

Keep rows = 5 but print each digit followed by a space.

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

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

            System.out.println();
        }
    }
}

How It Works

1. Same structure. Outer and inner bounds match Example 1 exactly.

2. Only the print changes. j + " " adds a trailing space after each digit.

3. Same order. Digit sequence is unchanged — only spacing differs.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of digits

If println is inside the inner loop, each digit lands on its own line. Use print for digits; println only after the inner loop.

Ascending outer

Growing triangle

Using i = 1..rows with j = i..1 grows the triangle upward instead of shrinking from the top.

Wrong inner start

Off-by-one rows

Starting j at rows instead of i prints the full descending line every time.

rows = 1

Single digit

Output is just 1 — the smallest non-empty case.

rows ≤ 0

Empty output

The outer loop never runs — print nothing or show a message.

Bad input

Use hasNextInt

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

Time and Space Complexity

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

Total digits = n + (n−1) + … + 1 = n(n+1)/2 — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: for i from rows down to 1, print j from i down to 1.
  • Both loops descend: outer shrinks the row; inner prints reverse digits.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for i = rows..1, print j = i..1, then println().

Frequently Asked Questions

The inner loop runs j from i down to 1, so each row prints i, i-1, …, 1.
Because rows = 5 and the outer loop starts at i = rows. The first row prints from 5 down to 1.
Program 2 prints i..rows (left-shifted ascending digits). Program 3 prints i..1 (reverse descending) with a shrinking outer loop.
Keep the descending outer loop but change the inner loop to j = 1..i (ascending).
Replace 5 with rows in the outer bound — see Example 2.
Use System.out.print(j + " ") instead of System.out.print(j) — see Example 3.
O(n²) for n rows. Total prints are n + (n-1) + … + 1 = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.

Did you know?

This pattern prints each row in descending order from i down to 1. The outer loop shrinks the row length while the inner loop counts downward — producing 54321, 4321, 321, and so on.

Next: Left-Aligned Descending Triangle

Move on to the left-aligned descending number triangle in the Java number-pattern series.

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