Inverted Right-Angled Triangle Star Pattern in Java

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

What Is This Pattern?

An inverted right-angled triangle star pattern prints a left-aligned upside-down staircase of * characters: the first row has rows stars, and each next row has one fewer down to 1.

Remember
Rule: on outer value i, print i stars (i counts down)

*****
****
***
**
*         ← 5 rows

In Java you solve it with two nested for loops: the outer loop counts from rows down to 1, the inner loop prints stars for the current i, then System.out.println() moves to the next line. It is the mirror of Program 1 — same inner loop, reversed outer direction.

How to Solve It

Two ways to emit the same shape — start with a countdown outer loop, then optionally use a forward formula or String.repeat (Java 11+).

MethodIdeaBest for
Countdown outeri = rows..1, inner prints i stars via System.out.printLearning, interviews, clearest invert of Program 1
Forward + formulai = 1..rows, print rows - i + 1 starsWhen you prefer ascending counters
"*".repeat(i)Build a whole row in one call while counting downShorter demos once loops click

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i:
        print "*" (no newline)
    print newline

Cheat sheet

GoalPattern
Countdown rowsfor (i = rows; i >= 1; i--)
Print i starsfor (j = 1; j <= i; j++) System.out.print("*");
End the rowSystem.out.println();
Forward equivalentfor (j = 1; j <= rows - i + 1; j++)
One-line row shortcutSystem.out.println("*".repeat(i)); while counting down
Upright versionfor (i = 1; i <= rows; i++) → Program 1

Write vs WriteLine

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

Same idea as C# Write / WriteLine: print stars without a newline, then end the row once.

Live Preview

Change the row count and the inverted triangle updates instantly — including the triangular star total.

Whole numbers from 1 to 20. Tap a chip or type a value — the preview redraws as you go. The first line has that many stars.

Live result 5 rows · 15 stars
*****
****
***
**
*

Worked Walkthrough — rows = 4

Trace each outer-loop value of i as it counts down, and count how many times the inner loop runs.

iInner jPrinted rowStars
41..4****4
31..3***3
21..2**2
11..1*1

Total star prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. Same triangular sum as Program 1 — time is still O(n²).

Java Programs

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

Example 1 — Fixed rows = 5

Hard-coded height with a countdown outer loop — the clearest invert of Program 1.

Java
class InvertedTriangle {
    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("*");
            }
            System.out.println();
        }
    }
}

How It Works

1. Set height. rows = 5 means the triangle has five lines; the first line has five stars.

2. Outer loop counts down. i runs from rows down to 1.

3. Inner loop prints stars. For each i, j runs from 1 to i, so the current row gets exactly i stars via System.out.print("*").

4. Break the line. System.out.println() after the inner loop starts the next (shorter) row.

When i = 5 you get *****; when i = 4 you get ****; and so on down to one star.

Example 2 — User Input Version

Read the row count at runtime with Scanner. Prefer hasNextInt in real apps (shown in the tip below).

Java
import java.util.Scanner;

class InvertedTriangleInput {
    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("*");
            }
            System.out.println();
        }

        sc.close();
    }
}

How It Works

1. Prompt and read. Ask for a row count, then sc.nextInt() stores the integer.

2. Same nested-loop core. Only the source of rows changes — the countdown print logic matches Example 1.

3. Safer input tip. nextInt() throws on letters. Prefer:

Safer input
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 (Java 11+)

Build each row in one call while counting down — same shape, no explicit inner star loop.

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

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

How It Works

1. One outer loop. Still walk i from rows down to 1.

2. Build the row. "*".repeat(i) creates a string of length i filled with stars.

3. Print and advance. println prints that string and ends the line.

Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

Wrong direction

Upright instead of inverted

If you use i = 1..rows instead of i = rows..1, you get Program 1’s growing triangle. For the inverted shape, count down — or use rows - i + 1 stars with a forward loop.

println inside

Column of stars

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

j <= rows

Rectangle, not triangle

Inner bound must be j <= i (or j <= rows - i + 1). j <= rows prints a filled rectangle.

No println

One endless line

Omitting the row break glues every star onto a single line.

rows = 1

Single star

Output is just * on one line — a good sanity check (same as Program 1).

rows ≤ 0

Empty output

Outer loop never runs. Validate and re-prompt for interactive programs.

Bad input

Check hasNextInt

nextInt() throws on letters — prefer sc.hasNextInt() first.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
String.repeat (Example 3)O(rows²)O(rows) per temporary row string

Total stars printed = n + (n-1) + … + 1 = n(n+1)/2, which is still quadratic in n — identical to Program 1.

Key Takeaways

  • Rule: outer value i prints exactly i stars, with i counting from rows down to 1.
  • Same as Program 1: only the outer loop direction changes; the inner star loop stays j = 1..i.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time from the triangular star count; O(1) extra space for nested loops.

One line: for i from rows down to 1, print i stars with print, then println.

Frequently Asked Questions

The outer loop runs i from rows down to 1. For each i, the inner loop prints i stars. The first output line uses i equal to rows so it is the longest; each later line has a smaller i, so the triangle points downward.
Program 1 uses for (i = 1; i <= rows; i++) so stars grow. This program uses for (i = rows; i >= 1; i--) so stars shrink. The inner loop still runs j from 1 to i.
Yes. Use for (i = 1; i <= rows; i++) and print (rows - i + 1) stars in the inner loop. Both styles produce the same shape.
System.out.print stays on the same line. System.out.println ends the current line. Stars use print; the row break uses println after the inner loop.
O(n²) for n rows. Total stars are still n(n+1)/2, same as the upright triangle.
Yes. System.out.println("*".repeat(i)) prints a full row in one call while i counts down (Java 11+).
Check sc.hasNextInt() before sc.nextInt() and require rows >= 1 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?

This inverted triangle uses the same inner loop as Program 1 — only the outer loop direction changes. Total stars stay n(n+1)/2, so complexity is still O(n²).

Next: Right-Aligned Triangle

Add leading spaces so the triangle leans to the right instead of the left.

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