Inverted Right-Aligned Triangle Star Pattern in Java

What You'll Learn
This program prints an inverted right-aligned triangle. The first row prints rows stars, and each next row prints one less star, while leading spaces increase to keep the right edge aligned.
For row i (from rows down to 1), print rows - i spaces and then print i stars.
⭐ 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 <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}🧠 How It Works
Setup
rows is the height. i counts rows from rows down to 1; j prints spaces; k prints stars.
Outer loop (rows, reverse)
for (int i = rows; i >= 1; i--) starts with the widest row (i == rows) and ends at a single star.
Padding: System.out.print(" ")
for (int j = 1; j <= rows - i; j++) prints rows - i spaces. On the first row that count is zero; it grows as i shrinks.
Stars: System.out.print("*")
for (int k = 1; k <= i; k++) prints i stars after the padding.
New line
System.out.println() ends each row. Characters before the newline: (rows - i) + i = rows every time.
Inverted right-aligned triangle
Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Wide rows scroll horizontally in the green preview on phones.
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 <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Compare with Program 3 to see how reversing the outer loop changes the shape
- Print a centered inverted pyramid by switching to odd stars + more spaces
- Print a hollow version (stars only on the borders)
- Use a different character (like
#) - Validate that
rows > 0
Avoid
- Forgetting that spaces increase as stars decrease (alignment breaks)
- Off-by-one errors in the space loop (
rows - i) - Mixing tabs and spaces (output alignment changes)
- Skipping newline after each row
- Assuming input is valid without checking
Key Takeaways
Inverted right-aligned = print (rows - i) spaces, then i stars, with i decreasing.
The outer loop runs backward (rows to 1) to shrink the star count each row.
Spaces increase by 1 each row, keeping the right edge aligned.
Time complexity is O(n²) for n rows.
This pattern is a stepping stone to centered inverted pyramids and diamond shapes.
❓ Frequently Asked Questions
1 to rows (Program 3). The space loop stays rows - i, but stars become i as i increases.Next: Pyramid Pattern
Continue to Program 5 to print a centered pyramid star pattern in Java.
Right-aligned patterns are excellent practice before moving to centered pyramids, because both require printing spaces and stars with the correct counts.
13 people found this page helpful
