Write a C++ program to print the hollow star pyramid pattern using for loop.
#include<iostream> using namespace std; int main() { int i, j, k, rows; cout << "Enter Hollow Pyramid Star Pattern Row = "; cin >> rows; cout << "Hollow Pyramid Star Pattern\n"; for(i = 1; i <= rows; i++) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = 1; k <= i * 2 - 1; k++) { if(k == 1 || k == i * 2 - 1 || i == rows) { cout << "*"; } else { cout << " "; } } cout << "\n"; } return 0; }
This C++ example prints the hollow 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 Pyramid Star Pattern Row = "; cin >> rows; cout << "Enter Symbol for Hollow Pyramid Pattern = "; cin >> ch; cout << "Hollow Pyramid Star Pattern\n"; for(i = 1; i <= rows; i++) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = 1; k <= i * 2 - 1; k++) { if(k == 1 || k == i * 2 - 1 || i == rows) { cout << ch; } else { cout << " "; } } cout << "\n"; } return 0; }
Enter Hollow Pyramid Star Pattern Row = 14
Enter Symbol for Hollow Pyramid Pattern = &
Hollow Pyramid Star Pattern
&
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
&&&&&&&&&&&&&&&&&&&&&&&&&&&