Write a C++ program to print the hollow inverted star pyramid pattern using for loop.
#include<iostream> using namespace std; int main() { int i, j, k, rows; cout << "Enter Hollow Inverted Pyramid Star Pattern Rows = "; cin >> rows; cout << "Hollow Inverted Pyramid Star Pattern\n"; for(i = rows; i > 0; i--) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = 1; k <= 2 * i - 1; k++) { if(i == 1 || i == rows || k == 1 || k == 2 * i - 1) { cout << "*"; } else { cout << " "; } } cout << "\n"; } return 0; }
This C++ example prints the hollow inverted pyramid pattern of a given character using a while loop.
#include<iostream> using namespace std; int main() { int i, j, k, rows; char ch; cout << "Enter Hollow Inverted Pyramid Star Pattern Rows = "; cin >> rows; cout << "Enter Symbol for Hollow Inverted Pyramid Pattern = "; cin >> ch; cout << "Hollow Inverted Pyramid Star Pattern\n"; i = rows; while( i > 0) { j = 1; while(j <= rows - i) { cout << " "; j++; } k = 1; while( k <= 2 * i - 1) { if(i == 1 || i == rows || k == 1 || k == 2 * i - 1) { cout << ch; } else { cout << " "; } k++; } cout << "\n"; i--; } return 0; }
Enter Hollow Inverted Pyramid Star Pattern Rows = 15
Enter Symbol for Hollow Inverted Pyramid Pattern = #
Hollow Inverted Pyramid Star Pattern
#############################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#