Inverted Right-Angled Triangle Star Pattern in Java

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

What You'll Learn

This program prints an inverted right-angled triangle where each next row contains one less star than the previous row.

Row i prints exactly i stars while i decreases from rows to 1.

⭐ Pattern Output

When you run the program with rows = 5:

Output
*****
****
***
**
*
1

Complete Java Program

Fixed rows = 5 version:

Java
public class Main {
    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

Entry point and variables

In main, rows fixes the demo height. The first printed line shows rows stars. i and j drive the outer and inner loops.

Setup
2

Outer loop (rows, reverse)

for (int i = rows; i >= 1; i--) starts at the widest row; each i is how many stars that line prints.

Row control
3

Inner loop: System.out.print("*")

for (int j = 1; j <= i; j++) prints i stars on one line.

Stars
4

New line

System.out.println() ends the row before i changes.

Line break
=

Inverted triangle

Decreasing i prints the wide line first. Total stars still n(n+1)/2O(n²) time, O(1) extra space. The widest line scrolls horizontally in the green preview on small screens.

2

Variation — User Input Version

Accept rows with Scanner:

Java
import java.util.Scanner;

public class Main {
    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();
    }
}

💡 Tips for Enhancement

Try These

  • Accept row count from the user with Scanner
  • Print the upright triangle (Program 1) to compare both shapes
  • Add spaces between stars for visual variety
  • Build a centered inverted pyramid (Program 6 idea) using leading spaces + odd stars
  • Create a hollow inverted triangle (print only border stars)

Avoid

  • Hard-coding the row count in real programs
  • Forgetting System.out.println() between rows
  • Using string concatenation in a tight loop for large patterns (prefer StringBuilder)
  • Not handling invalid user input
  • Closing System.in if you need input again later

Key Takeaways

1

Outer loop starts from rows and goes down to 1, so the triangle shrinks each row.

2

Row i prints exactly i stars, so the widest row comes first.

3

Total stars are n(n + 1)/2, so time complexity is O(n²).

4

This is the mirror of Program 1 and helps you practice loop bounds and decrementing loops.

5

Use Scanner to make the row count dynamic.

❓ Frequently Asked Questions

The outer loop runs from i = rows down to 1. For each row i, the inner loop runs from j = 1 to i, printing one star per iteration. So the first row prints rows stars and each next row prints one less.
Yes. Program 1 uses i = 1 .. rows. For the inverted version, just reverse it: i = rows .. 1.
The time complexity is O(n²) where n is the number of rows, because the total number of printed stars is \(1+2+\dots+n = n(n+1)/2\).
In small practice programs it’s fine to close it. In larger applications, closing a Scanner reading from System.in also closes the underlying stream, so avoid closing it if you plan to read input again later.

Next: Right-Aligned Triangle

Continue to Program 3 to print the right-aligned star triangle pattern in Java.

Program 3 →
Did you know?

This is the mirror of Program 1. Practicing both helps you master loop bounds and makes it easier to build larger patterns like pyramids and diamonds.

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.

15 people found this page helpful