Java Descending Number Triangle Pattern (Left-Aligned)

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

What Is This Pattern?

A left-aligned descending number triangle keeps the same starting digit on every row while the tail gets shorter — from rows..1 down to a single rows.

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

54321
5432
543
54
5     ← rows = 5

Unlike Program 3 (first digit changes each row), here every row starts at rows.

How to Solve It

Walk i from 1 to rows; on each row print j from rows down to i.

MethodIdeaBest for
Nested loopsOuter i = 1..rows; inner j = rows..i descendingLearning, interviews, exams
Spaced digitsSame loops; print each value with a trailing spaceReadable output when rows > 9

Pseudocode

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

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Descending digitsfor (int j = rows; j >= i; j--)
Print digitSystem.out.print(j);
End the rowSystem.out.println();
Digits per rowrows - i + 1

Printing Numbers vs Starting a New Line

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

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

Live Preview

Change the row count and the 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
5432
543
54
5

Worked Walkthrough — rows = 5

Trace how j always starts at 5 and stops at i.

iRange jCountPrinted row
15..1554321
25..245432
35..33543
45..4254
5515

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 — outer ascending, inner descending from rows.

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

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

How It Works

1. Outer loop. i is the lowest digit printed on that row.

2. Inner loop. j always starts at rows and counts down to i.

3. End the row. println() runs only after the inner loop finishes.

When i = 1: 5..1 → 54321. When i = 3: 5..3 → 543.

Example 2 — Rows Input

Read rows at runtime. Same nested-loop core.

Java
import java.util.Scanner;

public class LeftAlignedDescendingTriangleInput {
    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 = 1; i <= rows; i++) {
            for (int j = rows; j >= i; 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 Output

Same nested loops — each digit printed with a trailing space for readability.

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

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

How It Works

1. Same structure. Loops unchanged — only the print adds a space.

2. Why space? Digits stay readable when values reach two digits.

3. Width tip. For larger rows, prefer format("%2d ", j) for aligned columns.

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.

Wrong start

Looks like Program 3

Starting j at rows - i + 1 instead of rows changes the first digit each row.

Ascending j

Reversed digits

Using j++ from i to rows prints ascending rows like Program 5, not this pattern.

No println

One long line

Forgetting println() after the row glues every digit onto one endless line.

rows = 1

Single digit

Output is just 1 on one line.

Bad input

Use hasNextInt

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

Time and Space Complexity

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

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

Key Takeaways

  • Rule: row i prints j from rows down to i.
  • Fixed prefix: every row starts at rows; only the tail shortens.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, print j from rows down to i with print, then println().

Frequently Asked Questions

Because the inner loop always begins at j = rows. Only the stopping point changes as i grows, so each row starts with the maximum number.
The outer loop runs i from 1 to rows. For each row, the inner loop runs j from rows down to i and prints j, then println ends the row.
Program 3 prints 54321, then 4321, then 321 (the first digit changes). Program 4 prints 54321, then 5432, then 543 (the first digit stays at rows).
Yes. Print System.out.print(j + " ") in the inner loop — see Example 3.
O(n²) for n rows. Total printed numbers are n + (n-1) + … + 1 = n(n+1)/2.
Program 5 prints ascending rows 1, 12, 123. Program 4 prints descending digits from rows down to i on each row.
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?

The inner loop always starts at rows and counts down to i, so every row begins with the same digit while the tail shortens — 54321, 5432, 543, and so on.

Next: Ascending Number Triangle

Move on to the ascending number triangle in the Java number-pattern series.

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