Java Repeating Number Triangle Pattern (Inverted)

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

What Is This Pattern?

An inverted repeating number triangle starts widest and shortens each line — digit i repeats exactly i times, counting down from rows.

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

55555
4444
333
22
1          ← rows = 5

Same digit/count rule as Program 9, but the outer loop counts down so the peak digit appears first. Different from Program 10, where repeats grow while the digit falls.

How to Solve It

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

MethodIdeaBest for
Nested loopsOuter down, inner 1..i, print iLearning, interviews, exams
String.repeatOne println per rowWhen the bounds already make sense

Pseudocode

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

Cheat sheet

GoalPattern
Outer (shrink width)for (int i = rows; i >= 1; i--)
Inner (repeat count)for (int j = 1; j <= i; j++) System.out.print(i);
End the rowSystem.out.println();
Repeats on row ii (same as the digit)
ShortcutString.valueOf(i).repeat(i)
Vs Program 9Same rule; outer counts down instead of up

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit i
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 inverted repeating triangle updates instantly.

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

Live result rows = 5 · 15 digits
55555
4444
333
22
1

Worked Walkthrough

Trace three outer values when rows = 5 — watch digit and repeat count both equal i as it shrinks.

Outer iInner jRepeatsPrints
51..5555555
31..33333
11..111

The printed value is always the outer i; the inner loop only controls how many times it appears.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a String.repeat shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

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

Java
public class InvertedRepeatingTriangle {
    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(i);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer sets digit and width. i starts at rows and moves toward 1 — both the printed value and the repeat count.

2. Inner repeats the digit. j runs from 1 to i, so i appears i times.

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 InvertedRepeatingTriangleInput {
    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(i);
            }
            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 — String.repeat Shortcut

Build each row in one call once the i-times rule is clear.

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

        for (int i = rows; i >= 1; i--) {
            System.out.println(String.valueOf(i).repeat(i));
        }
    }
}

How It Works

1. Same outer loop. i still counts down from rows to 1.

2. Count equals digit. repeat(i) matches the nested inner loop that runs i times.

3. Learn loops first. Prefer Examples 1–2 in class; use repeat as a later shortcut (Java 11+).

Edge Cases & Pitfalls

Check these before calling the solution done.

Print value

Print i, not j

Printing j reprints an ascending digit run on each shrinking row, not a repeated-digit triangle.

Inner bound

Inner must run 1..i

Using rows..i instead reprints Program 10 (5, 44, 333), not this shape.

println inside

Do not put println inside the inner loop

That prints one digit per line and destroys the triangle.

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)
String.repeat (Example 3)O(n²)O(n) per row string

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

Key Takeaways

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

One line: print digit i exactly i times while i counts down from rows.

Frequently Asked Questions

An inverted repeating number triangle: for rows=5 you get 55555, 4444, 333, 22, 1.
The outer loop decreases i from rows to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times, so the widest row is first.
System.out.print(i) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Program 9 counts up (1, 22, 333). Program 11 uses the same digit/count rule but counts down, so the widest row comes first (55555, 4444, 333).
Program 10 uses inner rows..i so repeats grow while the digit falls (5, 44, 333). Program 11 uses inner 1..i so digit and count both equal i (55555, 4444, 333).
Yes. System.out.println(String.valueOf(i).repeat(i)) prints a full row in one call. Nested loops are better for learning; repeat is a handy shortcut later.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.

Did you know?

Row digit i repeats exactly i times. The outer loop counts down from rows, so the first row is widest (rows copies of rows) and each line shortens — still O(n²) total prints.

Next: Shrinking Repeating Pattern

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

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