How to print Hollow Pyramid Pattern in Java - Step by Step Examples
How to print Hollow Pyramid Pattern in Java ?
Answer
In Java, printing a hollow pyramid pattern involves similar nested loop logic. The outer loop manages rows, while inner loops control spaces and stars to create the hollow structure.
✐ Examples
1 Hollow Pyramid Pattern of height 5
In this example,
- Set the number of rows for the pyramid.
- Use nested loops: one for rows, one for spaces, and one for stars.
- In the inner loops, print spaces for the spaces between stars and print stars for the pyramid edges.
- Adjust conditions to print stars only at the pyramid edges and spaces elsewhere to create the hollow effect.
- Print each row to create the hollow pyramid pattern.
Java Program
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print stars for the pyramid
for (int k = 1; k < (2 * i); k++) {
if (k == 1 || k == (2 * i - 1) || i == rows) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in Java language with well detailed examples.
More Java Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in Java ?
- How to print Right Half Pyramid Pattern in Java ?
- How to print Pyramid Pattern in Java ?
- How to print Rhombus Pattern in Java ?
- How to print Diamond Pattern in Java ?
- How to print Hour Glass Pattern in Java ?
- How to print Hollow Square Pattern in Java ?
- How to print Hollow Pyramid Pattern in Java ?
- How to print Hollow Inverted Pyramid Pattern in Java ?
- How to print Hollow Diamond Pattern in Java ?
- How to print Floyd's Trianlge Pattern in Java ?
- How to print Pascal's Triangle Pattern in Java ?