Inverted Pyramid Star Pattern in Java

What You'll Learn
This program prints an inverted pyramid by decreasing the odd number of stars row by row, while increasing leading spaces to keep it centered.
Row i prints rows - i spaces and 2 * i - 1 stars (with i decreasing 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 <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}🧠 How It Works
Setup
rows is the height. i counts from rows down to 1; inner loops print spaces then stars like the upright pyramid, only the outer i loop runs backward.
Outer loop (rows, reverse)
for (int i = rows; i >= 1; i--) starts with i == rows (widest line, 2*rows-1 stars, no left margin) and ends at i == 1 (one star, rows - 1 spaces).
Margin: System.out.print(" ")
for (int j = 1; j <= rows - i; j++) prints rows - i spaces. As i decreases each row, that count grows so the shrinking star band stays centered.
Stars: System.out.print("*")
for (int j = 1; j <= 2 * i - 1; j++) prints an odd run that steps down (for rows = 5: 9, 7, 5, 3, 1). Same formula as Program 5; only i’s sequence changed.
New line
System.out.println() ends each row. Per-row character count is still (rows - i) + (2i - 1) = rows + i - 1.
Inverted pyramid
Total stars still n² over n = rows rows; O(n²) time, O(1) extra space. The first line is the widest—it scrolls sideways in the green preview on small viewports.
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 j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Print the upright pyramid (Program 5) to see the mirror effect
- Combine Program 5 + Program 6 to form a filled diamond
- Print a hollow inverted pyramid (stars only at edges)
- Use
StringBuilderfor very largerows - Validate that
rows > 0before printing
Avoid
- Forgetting to increase spaces as stars decrease
- Using even star counts (it won’t stay centered)
- Off-by-one errors in
2 * i - 1 - Mixing tabs and spaces (alignment changes)
- Assuming user input is always valid
Key Takeaways
Row i prints rows - i spaces followed by 2 * i - 1 stars.
Star counts shrink by 2 each row, keeping symmetry around the center.
This is the mirror of Program 5 (pyramid).
Time complexity is O(n²) for n rows.
Pyramid + inverted pyramid = filled diamond (Program 10).
❓ Frequently Asked Questions
rows - 1 to avoid repeating the middle row (Program 10).Next: Inverted V Hollow Pattern
Continue to Program 7 to print an inverted V-shaped hollow star pattern in Java.
This inverted pyramid is used as the lower half for the filled diamond (Program 10) when you print it after the upright pyramid.
12 people found this page helpful
