Inverted Pyramid Star Pattern in Java

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

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:

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 <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

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.

Setup
2

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).

Direction
3

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.

Centering
4

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.

Width
5

New line

System.out.println() ends each row. Per-row character count is still (rows - i) + (2i - 1) = rows + i - 1.

Line break
=

Inverted pyramid

Total stars still 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.

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 <= 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 StringBuilder for very large rows
  • Validate that rows > 0 before 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

1

Row i prints rows - i spaces followed by 2 * i - 1 stars.

2

Star counts shrink by 2 each row, keeping symmetry around the center.

3

This is the mirror of Program 5 (pyramid).

4

Time complexity is O(n²) for n rows.

5

Pyramid + inverted pyramid = filled diamond (Program 10).

❓ Frequently Asked Questions

Odd star counts keep the pyramid centered around one middle column. Each next row removes two stars, shrinking evenly on both sides.
Print Program 5 (pyramid) and then print this inverted pyramid starting from rows - 1 to avoid repeating the middle row (Program 10).
It’s O(n²) for n rows because each row prints Theta(n) characters and there are n rows.

Next: Inverted V Hollow Pattern

Continue to Program 7 to print an inverted V-shaped hollow star pattern in Java.

Program 7 →
Did you know?

This inverted pyramid is used as the lower half for the filled diamond (Program 10) when you print it after the upright pyramid.

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