Pyramid Star Pattern in Java
Java program to print a full pyramid or equilateral triangle star pattern.
Last updated: 11 January 2026
Method 1: Using Loops
Handling spaces and stars.
Main.javaRun in Compiler →
public class Pyramid {
public static void main(String[] args) {
int rows = 5, k = 0;
for (int i = 1; i <= rows; ++i, k = 0) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
while (k != 2 * i - 1) {
System.out.print("* ");
++k;
}
System.out.println();
}
}
}Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *Explanation
We need to print spaces first (rows-i), then odd number of stars (2*i-1).
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler