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.
In this example,
#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;
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in C++ language with well detailed examples.