Left-Aligned Descending Number Triangle in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Descending Loops

What You’ll Learn

The left-aligned descending number triangle prints 54321, 5432, 543, 54, 5 — a natural step after Program 3 where the first digit changes each row. This tutorial covers ascending outer and descending inner loops, a live preview, algorithm steps, worked Java examples, edge cases, and complexity.

Shape Rule

rows..i per row

Row 1 prints 54321, row 2 prints 5432, shrinking the tail while the first digit stays rows.

Outer Loop

1..rows

for (i = 1; i <= rows; i++) moves the inner-loop stop forward each row.

Inner Loop

rows..i descending

for (j = rows; j >= i; j--) always starts at rows and counts down to i.

print vs println

Same line / next line

Digits use System.out.print(j); end each row with System.out.println().

Live Preview

1–20 rows

Pick a row count and draw the left-aligned descending triangle in the browser.

O(n²)

Complexity

Total digit prints = n(n+1)/2; extra memory stays O(1).

Introduction

A left-aligned descending number triangle keeps the same starting digit on every row while the tail gets shorter. With rows = 5, the output is 54321, 5432, 543, 54, 5.

In Java the outer loop runs i = 1..rows, the inner loop prints j from rows down to i, then System.out.println() moves to the next line.

Why it matters?

It teaches how changing the inner-loop start (always rows) creates a fixed-prefix triangle — compare with Program 3 next.

Key Highlights

Fixed prefix

Every row starts at rows.

Shrinking tail

Inner loop j = rows..i shortens each row.

vs Program 3

Program 3 changes the first digit; Program 4 keeps it at rows.

Series Foundation

Follow Program 3; continue to Program 5 (ascending triangle) next.

In short: for each i from 1 to rows, print j from rows down to i, then System.out.println().

📝 Problem & Approach

Given a positive integer rows (e.g. 5), print a left-aligned descending triangle: each row prints digits from rows down to i, with the outer loop counting from 1 up to rows.

Java
// rows = 5 (conceptual shape)
// 54321
// 5432
// 543
// 54
// 5

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines — outer loop runs from 1 up to rows.
iintOuter loop — current row index; sets where the inner loop stops.
jintInner loop — descending from rows down to i; always starts at the max digit.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Nested loops54321, 5432, …Learning and interviews
User-input rowssc.nextInt();Flexible console programs
Spaced outputSystem.out.print(j + " ")Easier reading per row

⚡ Quick Reference

GoalPattern
Walk rowsfor (i = 1; i <= rows; i++)
Print digits rows..ifor (j = rows; j >= i; j--) System.out.print(j);
End the rowSystem.out.println();
Spaced digitsSystem.out.print(j + " ");
User inputsc.nextInt();
Program 3 contrastfor (i = rows; i >= 1; i--) with j = i..1

📋 Outer Asc vs Inner Desc vs Combined

Same triangle — how the two loop directions work together.

Outer (ascending)
i = 1..rows

Moves the inner stop forward — shortens each row

Inner (descending)
j = rows..i

Always starts at max digit, counts down to i

Fixed prefix
starts at rows

Every row begins with the same first digit

Learning tip
compare P3

Swap inner start from i to rows to get this shape

Context

When This Pattern Shows Up

Reach for this pattern when teaching descending inner loops and shrinking row lengths.

  1. Post left-shift exercise

    Natural follow-up after Program 3 — fixed first digit with a shrinking tail.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with Scanner for a flexible row count.

  4. Gateway to variants

    Compare with Program 3 (left-shifted), then continue to Program 5 (ascending).

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Enter a row count and draw the left-aligned descending triangle in the browser.

Try 5, 7, or 10. Larger values still work up to 20.

Live result
Press "Draw pattern".

Examples Gallery

Three complete Java programs — fixed rows, user input, and spaced output variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows with ascending outer and descending inner loops.

Example 1 — Fixed rows = 5

Hard-coded row count — inner loop always starts at 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

When i = 1, the inner loop prints 5, 4, 3, 2, 1 — output 54321. When i = 5, only 5 prints. The outer loop increases i to shorten each row.

📈 User Input

Read the row count with Scanner instead of hard-coding 5.

Example 2 — User Input Version

Read rows with Scanner.nextInt(); both loops use the input as the max digit.

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

        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

Same nested-loop core as Example 1; only the source of rows changes. Non-numeric input throws InputMismatchException — check hasNextInt() for safer labs.

⚡ Spaced Output

Add a space between digits for easier reading on each row.

Example 3 — Spaced Output

Append a space after each digit for easier reading.

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

Same loop structure; only System.out.print(j + " ") adds spacing between digits.

🧠 How the Algorithm Prints Rows

1

Set up

System.out is built in; use Scanner when reading input. Set loop variables i, j with rows = 5.

Setup
2

Outer loop walks rows

for (i = 1; i <= rows; i++) — ascending outer loop moves the inner stop forward each row.

Row
3

Inner loop (j)

for (j = rows; j >= i; j--) — prints digits from rows down to i.

Descend
4

New line

System.out.println() ends the row after the inner loop finishes.

Break
=

Left-aligned descending triangle complete

Tail shortens while the first digit stays at rowsO(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value of i, the inner-loop range, digit count, and full row output.

iInner loop (j)PrintsRow output
15, 4, 3, 2, 1554321
25, 4, 3, 255432
35, 4, 35543
45, 4554
5555

Prints per row = rows - i + 1 — total prints = n(n+1)/2 for n rows.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: flip j-- to j++ and watch digit order change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: continue to Program 5 for an ascending number triangle.

3. Console Formatting Drills

Practice System.out.print vs System.out.println() without complex math.

Example: put System.out.println() inside the inner loop by mistake.

4. Spaced Output

Add spaces between digits once the two-loop structure works.

Example: use System.out.print(j + " ") between digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).

6. Input Validation Labs

Pair the pattern with Scanner return checks and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner Java courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row shortens by one digit.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Match Descending Bounds

    Outer loop counts down (i--); inner loop must also count down from i to 1.

  2. 2. Prefer Scanner

    Call sc.hasNextInt() so bad input does not leave rows uninitialized.

  3. 3. Keep System.out.println() Outside

    Only call System.out.println() after the inner loop finishes the row.

  4. 4. Count Down in the Inner Loop

    for (j = rows; j >= i; j--) prints digits rows..i in reverse order.

  5. 5. Dry-Run rows = 3

    Trace i = 1, 2, 3 on paper before coding the full rows = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put System.out.println() inside the inner loop.

Common Pitfalls

Mistakes that commonly break left-aligned descending number triangles.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use System.out.print(j) for digits; System.out.println() only after the inner loop.

  2. 2. Wrong Inner Direction

    for (j = 1; j <= i; j++) prints ascending digits — you get Program 1’s shape, not this one.

    → Keep for (j = rows; j >= i; j--) so each row reads rows..i.

  3. 3. Descending Outer Loop

    for (i = rows; i >= 1; i--) changes the first digit each row — you get Program 3’s shape.

    → Use for (i = 1; i <= rows; i++) so every row starts at rows.

  4. 4. Wrong Inner Start

    j = i on every row prints a left-shifted tail — that is Program 3, not this pattern.

    → Start the inner loop at the fixed top value: j = rows.

  5. 5. Unchecked Scanner input

    Letters or empty input leave rows uninitialized.

    → Call sc.hasNextInt() and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single digit row

Output is just 1 on one line.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 21 and 2.

Bad input

Non-numeric Scanner input

Unchecked Scanner input leaves rows unset — call hasNextInt() first.

Large rows

Large row count

Each row prints i digits — total work grows as n(n+1)/2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Classic descending triangle

  • Outer loop counts up; inner prints 1..i
  • Review Program 1

2. Left-shifted triangle

  • Compare with Program 3
  • Descending outer loop; inner prints i..1

3. Ascending triangle

  • Continue with Program 5
  • Outer loop grows rows; inner prints 1..i

4. Spaced output

  • Use System.out.print(j + " ") between digits
  • Same loops, wider visual spacing

Notes

  • Left-aligned rule. Outer loop: i = 1..rows. Inner loop: j = rows..i with j--.
  • System.out.print stays on the line; System.out.println() advances — mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single 1.
  • Row i prints exactly rows - i + 1 digits — compare with Program 3 where each row prints i digits.

Quick Takeaway: outer loop i = 1..rows, inner loop j = rows..i with System.out.print(j), then System.out.println().

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Smaller demo (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The left-aligned descending number triangle is a compact nested-loop lesson: an ascending outer loop moves the inner stop forward while the inner loop prints digits from rows down to i. Master the fixed-rows version, then try user input and spaced output.

Practice the three examples above, then continue to Program 5 for the ascending number triangle.

Row i prints rows..i — keep System.out.print(j) for digits and System.out.println() for the break, and validate row counts when reading input.

💡 Best Practices

✅ Do

  • Use for (i = 1; i <= rows; i++) in the outer loop
  • Inner: for (j = rows; j >= i; j--) prints digits in reverse
  • Use System.out.print(j) for digits and System.out.println() after each row
  • Validate rows ≥ 1 for interactive programs
  • Call sc.hasNextInt() before using rows

❌ Don’t

  • Call System.out.println() inside the inner digit loop
  • Use ascending inner loop when you meant reverse order
  • Use descending outer loop when you meant a fixed first digit
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this left-aligned descending triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer loop

Counts up rows

Code
03

Inner loop

j = rows down to i

Code
04

Newline

Ends each row

Shape
O 05

Complexity

O(n²) time

Analysis

❓ 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 for spaced output — 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.

Continue to Program 5

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