Java Descending Number Triangle Pattern

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

What Is This Pattern?

A descending number triangle starts with the longest row and shortens by one digit each line — so you see 12345, 1234, 123, and so on down to 1.

Remember
Rule: outer i = rows..1; inner j = 1..i; print j

12345
1234
123
12
1          ← rows = 5

The outer loop counts down so the first row is widest. The next pattern, Program 2, keeps the same length idea but shifts the start digit leftward instead.

How to Solve It

Outer loop from rows down to 1. Inner loop from 1 to current i, printing each digit. End the line after the inner loop.

MethodIdeaBest for
Nested loopsOuter down, inner 1..iLearning, interviews, exams
StringBuilderAppend digits, then println the rowWhen you want one print per row

Pseudocode

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

Cheat sheet

GoalPattern
Outer (shrink width)for (int i = rows; i >= 1; i--)
Inner (print 1..i)for (int j = 1; j <= i; j++) System.out.print(j);
End the rowSystem.out.println();
Spaced digitsSystem.out.print(j + " ");
One print per rowAppend to StringBuilder, then println(row)
Ascending insteadOuter i = 1..rows (same inner)

Printing Numbers vs Starting a New Line

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

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

Live Preview

Change the row count and the descending triangle updates instantly.

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

Live result rows = 5 · 15 digits
12345
1234
123
12
1

Worked Walkthrough

Trace three outer values when rows = 5 — watch the row width shrink while digits still start at 1.

Outer iInner jPrints
51..512345
31..3123
11..11

Digits always begin at 1; only the stopping value i moves down as the outer loop counts down.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a StringBuilder row builder. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer down from 5, inner prints 1..i.

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

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

How It Works

1. Outer shrinks the width. i starts at rows and moves toward 1 — that is how many digits each row gets.

2. Inner prints ascending digits. j runs from 1 to i with System.out.print(j).

3. End the row. Call System.out.println() only after the inner loop finishes.

Example 2 — User Input Rows

Read the row count at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
import java.util.Scanner;

public class DescendingNumberTriangleInput {
    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 = rows; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Same loop core. Only the source of rows changes from a literal to Scanner.

2. Entering 4 stops early. You get four rows ending at a single 1.

3. Validate in real apps. Prefer checking hasNextInt() and requiring a positive height (tip below).

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

Example 3 — StringBuilder Row Builder

Append each digit, then print the full row once with println.

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

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

How It Works

1. Same bounds. Outer i = rows..1 and inner j = 1..i match Example 1.

2. Build, then print. row.append(j) collects digits; println(row) prints the whole line.

3. Same shape. Useful when exams want one print per row — loop bounds stay visible either way.

Edge Cases & Pitfalls

Check these before calling the solution done.

Outer direction

Count down with i--

Counting up from 1 to rows prints an ascending triangle (1, 12, 123), not this shape.

println inside

Do not put println inside the inner loop

That prints one digit per line and destroys the triangle.

Missing println

Always end the row

Omitting println glues every digit onto a single endless line.

Bad input

Validate with hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
StringBuilder (Example 3)O(n²)O(n) per row buffer

Digits printed are n + (n − 1) + … + 1 = n(n + 1) / 2 — quadratic in n. Direct prints need only the loop variables; StringBuilder adds a short-lived row buffer.

Key Takeaways

  • Rule: outer i = rows..1, inner j = 1..i, print j.
  • Shrink: each row drops one digit from the right while still starting at 1.
  • Break the row: call println only after the inner loop finishes.
  • Complexity: O(n²) time; O(1) extra space with direct prints.

One line: start at full width, print 1..i, then drop one digit each next row.

Frequently Asked Questions

A descending number triangle: for rows=5 you get 12345, 1234, 123, 12, 1.
Counting down makes the first row the longest. for (i = rows; i >= 1; i--) sets i to the full width first, then shrinks by one each line.
System.out.print(j) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Change the outer loop to for (i = 1; i <= rows; i++). Keep the inner loop as for (j = 1; j <= i; j++) System.out.print(j).
Program 1 prints 1..i with outer counting down (12345, 1234…). Program 2 starts each row at i and prints i..rows (12345, 2345…).
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt(), require rows ≥ 1, and reject non-numeric input — see Example 2.
One row prints a single digit 1.

Did you know?

Row i prints digits 1 through i. The outer loop counts down from rows, so the first line is longest and each row shortens by one digit — still O(n²) total prints.

Next: Left-Shifted Number Triangle

Continue with the next pattern in the Java number-pattern series.

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