Inverted Right-Angled Triangle Star Pattern in Java

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:
*****
****
***
**
*Complete Java Program
Fixed rows = 5 version:
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
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.
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.
Inner loop: System.out.print("*")
for (int j = 1; j <= i; j++) prints i stars on one line.
New line
System.out.println() ends the row before i changes.
Inverted triangle
Decreasing i prints the wide line first. Total stars still n(n+1)/2 — O(n²) time, O(1) extra space. The widest line scrolls horizontally in the green preview on small screens.
Variation — User Input Version
Accept rows with Scanner:
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.inif you need input again later
Key Takeaways
Outer loop starts from rows and goes down to 1, so the triangle shrinks each row.
Row i prints exactly i stars, so the widest row comes first.
Total stars are n(n + 1)/2, so time complexity is O(n²).
This is the mirror of Program 1 and helps you practice loop bounds and decrementing loops.
Use Scanner to make the row count dynamic.
❓ Frequently Asked Questions
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.i = 1 .. rows. For the inverted version, just reverse it: i = rows .. 1.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.
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.
15 people found this page helpful
