Pyramid Star Pattern in Java

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:
*
***
*****
*******
*********Complete Java Program
Fixed rows = 5 version:
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
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.
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.
Margin: System.out.print(" ")
for (int j = 1; j <= rows - i; j++) prints rows - i spaces so the odd-width star block sits centered.
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.
New line
System.out.println() ends the row. Row i prints (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.
Symmetric pyramid
Total stars n² (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.
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 = 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
StringBuilderfor largerowsto reduce I/O calls - Print a hollow pyramid (stars only at the edges)
- Validate
rows > 0before 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
Centering uses rows - i spaces before the stars.
Row i prints 2 * i - 1 stars (odd counts keep symmetry).
Each next row adds 2 stars and removes 1 leading space.
Time complexity is O(n²) for n rows.
This is the building block for diamonds (pyramid + inverted pyramid).
❓ Frequently Asked Questions
Next: Inverted Pyramid Pattern
Continue to Program 6 to print the inverted pyramid star pattern in Java.
If you print this pyramid and then print Program 6, you get a filled diamond (Program 10).
13 people found this page helpful
