Write a C++ Program to Print Inverted Star Pyramid with an example. This C++ program allows us to enter the rows and the symbol to print in the inverted pyramid pattern. Next, the nested for loops will iterate between total rows and 1 to print the * (or given symbol). Here, if k != (2 * i – 1) condition is true, print * otherwise, print empty space.
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
char ch;
cout << "\nPlease Enter Inverted Pyramid Star Pattern Rows = ";
cin >> rows;
cout << "\nPlease Enter Any Symbol to Print = ";
cin >> ch;
cout << "\n-----Inverted Pyramid Star Pattern-----\n";
for ( i = rows ; i >= 1; i-- )
{
for ( j = 0 ; j <= rows - i; j++ )
{
cout << " ";
}
for(k = 0; k != (2 * i - 1); k++)
{
cout << ch;
}
cout << "\n";
}
return 0;
}
C++ Program to Print Inverted Star Pyramid using a While Loop
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
char ch;
cout << "\nPlease Enter Inverted Pyramid Star Pattern Rows = ";
cin >> rows;
cout << "\nPlease Enter Any Symbol to Print = ";
cin >> ch;
cout << "\n-----Inverted Pyramid Star Pattern-----\n";
i = rows ;
while(i >= 1 )
{
j = 0 ;
while( j <= rows - i)
{
cout << " ";
j++ ;
}
k = 0;
while (k != (2 * i - 1))
{
cout << ch;
k++;
}
cout << "\n";
i--;
}
return 0;
}