How to print Hollow Pyramid Pattern in C++ - Step by Step Examples
How to print Hollow Pyramid Pattern in C++ ?
Answer
To print a hollow pyramid pattern in C++, you can use loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
✐ Examples
1 Hollow Pyramid Pattern (Height: 5) in C++
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.
C++ Program
#include <iostream>
using namespace std;
void printHollowPyramid(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
cout << " ";
}
for (int k = 1; k < (2 * i); k++) {
if (k == 1 || k == (2 * i - 1) || i == rows) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
}
int main() {
printHollowPyramid(5);
return 0;
}
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in C++ language with well detailed examples.
More C++ Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in C++ ?
- How to print Right Half Pyramid Pattern in C++ ?
- How to print Pyramid Pattern in C++ ?
- How to print Rhombus Pattern in C++ ?
- How to print Diamond Pattern in C++ ?
- How to print Hour Glass Pattern in C++ ?
- How to print Hollow Square Pattern in C++ ?
- How to print Hollow Pyramid Pattern in C++ ?
- How to print Hollow Inverted Pyramid Pattern in C++ ?
- How to print Hollow Diamond Pattern in C++ ?
- How to print Floyd's Trianlge Pattern in C++ ?
- How to print Pascal's Triangle Pattern in C++ ?