Patterns
Java Program to Print Pyramid Star Pattern
A Java program to print a pyramid star pattern.
Problem Description
Write a Java program to print a pyramid star pattern.
Code
public class PyramidPattern {
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
- Spaces: Calculate spaces based on the row number.
- Stars: Print
2 * i - 1stars for each rowi.
