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 a centered pyramid by printing spaces first and then an odd number of stars.

Row i prints rows - i spaces and 2 * i - 1 stars.

⭐ 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 = 1; i <= rows; 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 pyramid height. i indexes rows 1…rows; two inner loops both use j—first for spaces, then for stars. All loops sit inside main.

Setup
2

Outer loop (rows)

for (int i = 1; i <= rows; i++) walks from one star on row 1 to a bottom row of 2 * rows - 1 stars.

Row control
3

Margin: System.out.print(" ")

for (int j = 1; j <= rows - i; j++) prints rows - i spaces so the odd-width star block sits centered.

Centering
4

Stars: System.out.print("*")

for (int j = 1; j <= 2 * i - 1; j++) prints 1, 3, 5, …, 2*rows-1 asterisks—always odd so the pyramid has one peak star.

Width
5

New line

System.out.println() ends the row. Row i prints (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.

Line break
=

Symmetric pyramid

Total stars (sum of odd lengths 1 through 2n-1); O(n²) output for n = rows, O(1) extra space. The bottom row scrolls horizontally in the green preview on narrow screens. The same core loops form the top half of the solid diamond in Program 10.

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 = 1; i <= rows; 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 inverted pyramid by reversing the outer loop and adjusting star counts (Program 6)
  • Combine pyramid + inverted pyramid to form a filled diamond
  • Use StringBuilder for large rows to reduce I/O calls
  • Print a hollow pyramid (stars only at the edges)
  • Validate rows > 0 before printing

Avoid

  • Using even star counts (the pyramid won’t stay centered)
  • Forgetting spaces before stars (pattern shifts left)
  • Off-by-one mistakes in 2 * i - 1
  • Mixing tabs and spaces (alignment changes by font)
  • Assuming user input is always valid

Key Takeaways

1

Centering uses rows - i spaces before the stars.

2

Row i prints 2 * i - 1 stars (odd counts keep symmetry).

3

Each next row adds 2 stars and removes 1 leading space.

4

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

5

This is the building block for diamonds (pyramid + inverted pyramid).

❓ Frequently Asked Questions

Odd star counts (\(1,3,5,\dots\)) keep the pyramid perfectly centered around a single middle column. Each new row adds two stars, expanding equally on both sides.
Print stars only at the left and right edges of each row, and print a full row of stars at the base. Everything else becomes spaces.
It’s O(n²) for n rows because each row prints Theta(n) characters and there are n rows.

Next: Inverted Pyramid Pattern

Continue to Program 6 to print the inverted pyramid star pattern in Java.

Program 6 →
Did you know?

If you print this pyramid and then print Program 6, you get a filled diamond (Program 10).

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.

13 people found this page helpful