Inverted Pyramid
Program to print inverted pyramid pattern
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;
}Enter number of rows: 5
*********
*******
*****
***
*Understanding Inverted Pyramid
This program teaches you how to print an inverted pyramid pattern using nested loops in C++. An inverted pyramid is the reverse of a full pyramid — it starts with the largest row at the top and decreases to a single star at the bottom. This pattern helps beginners understand reverse iteration and how to modify loop conditions to create different patterns.
---
1. What is an Inverted Pyramid?
An inverted pyramid is a pattern that looks like this (for 5 rows):
*
***
*
***
*
Notice:
---
2. Header File
#include <iostream>
Provides cout for output and cin for input.
---
3. Variable Declaration
int rows;
Stores the number of rows in the inverted pyramid.
---
4. Taking Input
cin >> rows;
Gets the number of rows from the user.
---
5. Understanding Reverse Iteration
The key difference from a full pyramid is the
outer loop direction
:
-
Full pyramid
: for (int i = 1; i <= rows; i++) → counts up (1, 2, 3, 4, 5)
-
Inverted pyramid
: for (int i = rows; i >= 1; i--) → counts down (5, 4, 3, 2, 1)
This reverse iteration makes the pattern start large and get smaller.
---
6. Pattern Formulas
The formulas are the same as a full pyramid, but applied in reverse:
1.
Spaces
: rows - i spaces on the left
2.
Stars
: 2 * i - 1 stars in each row
Why it works in reverse:
i = rows (top row): Maximum stars, minimum spacesi = 1 (bottom row): Minimum stars, maximum spaces---
7. Step-by-Step Breakdown
Let's trace through when rows = 5:
Row 1 (i = 5, top row):
rows - i = 5 - 5 = 0 spaces2 * i - 1 = 2 * 5 - 1 = 9 stars*` (0 spaces + 9 stars)
Row 2 (i = 4):
5 - 4 = 1 space2 * 4 - 1 = 7 stars***` (1 space + 7 stars)
Row 3 (i = 3):
5 - 3 = 2 spaces2 * 3 - 1 = 5 stars*` (2 spaces + 5 stars)
Row 4 (i = 2):
5 - 2 = 3 spaces2 * 2 - 1 = 3 stars *** (3 spaces + 3 stars)Row 5 (i = 1, bottom row):
5 - 1 = 4 spaces2 * 1 - 1 = 1 star * (4 spaces + 1 star)---
8. Code Explanation
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
// Print stars
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
// Print spacesOuter Loop
:
rows and counts down to 1i-- decreases the value each iterationFirst Inner Loop
(Spaces):
j <= rows - ii = rows: rows - i = 0 → no spacesi = 1: rows - i = rows - 1 → maximum spacesSecond Inner Loop
(Stars):
j <= 2 * i - 1i = rows: Maximum stars (top row)i = 1: Minimum stars (bottom row)---
9. Visual Representation
For `rows = 5`:
Row 1 (i=5): [0 spaces] [9 stars] →
*
Row 2 (i=4): [1 space] [7 stars] →
***
Row 3 (i=3): [2 spaces] [5 stars] →
*
Row 4 (i=2): [3 spaces] [3 stars] → ***
Row 5 (i=1): [4 spaces] [1 star] → *
---
10. Comparison: Full vs Inverted Pyramid
| Aspect | Full Pyramid | Inverted Pyramid |
|--------|--------------|------------------|
| Loop direction | i = 1; i <= rows; i++ | i = rows; i >= 1; i-- |
| Top row | 1 star | Maximum stars |
| Bottom row | Maximum stars | 1 star |
| Spaces | Decrease down | Increase down |
| Stars | Increase down | Decrease down |
---
11. Key Concepts Demonstrated
1.
Reverse Iteration
: Using i-- to count backwards is essential for many patterns.
2.
Same Formulas, Different Direction
: The space and star formulas work the same, but the loop direction creates the inverted effect.
3.
Decreasing Patterns
: Understanding how to create patterns that shrink instead of grow.
4.
Centered Output
: Maintaining center alignment even when the pattern is inverted.
---
12. Common Mistakes
1.
Wrong loop direction
: Using i++ instead of i--
2.
Wrong starting value
: Starting at 1 instead of rows
3.
Wrong condition
: Using i <= rows instead of i >= 1
4.
Confusing space formula
: The space formula rows - i still works, but behaves differently because i is decreasing
---
13. Practical Applications
Inverted pyramids are used in:
---
Summary
rows to 1 using i--.(rows - i) spaces (increases as we go down).(2 * i - 1) stars (decreases as we go down).endl moves to the next line.This program is essential for understanding reverse iteration and how to create decreasing patterns. Combined with a full pyramid, you can create more complex patterns like diamonds.
Let us now understand every line and the components of the above program.
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.
Practical Learning Notes for Inverted Pyramid
This C++ program is part of the "Pattern Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.