Inverted Pyramid
Program to print inverted pyramid pattern
IntermediateTopic: Pattern Programs
C++ Inverted Pyramid Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = rows; i >= 1; i--) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
// Print stars
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}Output
Enter number of rows: 5
*********
*******
*****
***
*Understanding Inverted Pyramid
An inverted pyramid is the reverse of a full pyramid. We iterate from rows down to 1. For row i, we print (rows - i) spaces, then (2*i - 1) stars. The number of stars decreases as we go down.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.