Write the Code
Add the following code to the pyramid.cpp
file.
//Cpp Reverse Half Pyramid Pattern Using Asterix
//Nested Loop Structure
#include <iostream>
using namespace std;
int main()
{
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to print a Reverse Half Pyramid using * ===== \n\n";
//i to iterate the outer loop and j for the inner loop
int i, j, rows;
cout << "Enter the number of rows in the pyramid: ";
cin >> rows;
cout << "\n\nThe required Reverse Pyramid pattern containing " << rows << " rows is:\n\n";
//outer loop is used to move to a particular row
for (i = 1; i <= rows; i++)
{
//to display that the outer loop maintains the row number
cout << "Row ## " << i << " contains " << (rows - i + 1) << " stars : ";
//inner loop is used to decide the number of * in a particular row
for (j = rows; j >= i; j--)
{
cout << "* ";
}
cout << endl;
}
cout << "\n\n";
return 0;
}
The program takes the number of rows rows
as input and displays the reverse half pyramid up to the number of rows entered by the user using *
.