Pyramid Star Pattern in Java

Pyramid Star Pattern in Java

In this tutorial, we will learn how to print a pyramid star pattern in Java. This pattern is a centered triangle made up of stars and is a step ahead in complexity compared to the right-angled triangle.

🔺 What is a Pyramid Pattern?

It's a symmetrical triangle with stars, centered using spaces on the left side to align the pattern properly.

Example for n = 5:

    *
   ***
  *****
 *******
*********

✅ Java Program


public class PyramidPattern {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }

            // Print stars
            for (int k = 1; k <= (2 * i - 1); k++) {
                System.out.print("*");
            }

            System.out.println();
        }
    }
}

🧠 Interview Tip

Observe how the number of spaces decreases and the number of stars increases with each row. Understanding this balance is key to building advanced patterns.

🎯 Output

    *
   ***
  *****
 *******
*********

📌 Conclusion

We have implemented a pyramid star pattern in Java using nested loops. Patterns like these help in mastering loop structures, which are critical in interviews and logic-based problems.