To print a hollow inverted pyramid pattern in C++, we use nested loops to manage spaces and stars.
In this example,
#include <iostream>
int main() {
int numRows;
numRows = 5;
for (int row = numRows; row >= 1; row--) {
for (int space = 0; space < numRows - row; space++) {
std::cout << " ";
}
if (row == numRows || row == 1) {
for (int space = 1; space <= 2 * row - 1; space++) {
std::cout << "*";
}
} else {
std::cout << "*";
for (int space = 1; space <= 2 * row - 3; space++) {
std::cout << " ";
}
if (row != 1) {
std::cout << "*";
}
}
std::cout << std::endl;
}
return 0;
}
********* * * * * * * *
In this tutorial, we learned How to print Hollow Inverted Pyramid Pattern in C++ language with well detailed examples.