Inverted Pyramid

Program to print inverted pyramid pattern

IntermediateTopic: Pattern Programs
Back

C++ Inverted Pyramid Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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

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:

Starts with the maximum number of stars at the top
Each row has fewer stars than the row above
Stars are centered (spaces on the left)
Forms an upside-down triangle

---

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

`cout << "Enter number of rows: ";`

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:

When i = rows (top row): Maximum stars, minimum spaces
When i = 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):

Spaces: rows - i = 5 - 5 = 0 spaces
Stars: 2 * i - 1 = 2 * 5 - 1 = 9 stars
Output: `

*` (0 spaces + 9 stars)

Row 2 (i = 4):

Spaces: 5 - 4 = 1 space
Stars: 2 * 4 - 1 = 7 stars
Output: `

***` (1 space + 7 stars)

Row 3 (i = 3):

Spaces: 5 - 3 = 2 spaces
Stars: 2 * 3 - 1 = 5 stars
Output: `

*` (2 spaces + 5 stars)

Row 4 (i = 2):

Spaces: 5 - 2 = 3 spaces
Stars: 2 * 2 - 1 = 3 stars
Output: *** (3 spaces + 3 stars)

Row 5 (i = 1, bottom row):

Spaces: 5 - 1 = 4 spaces
Stars: 2 * 1 - 1 = 1 star
Output: * (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 spaces

Outer Loop

:

Starts at rows and counts down to 1
i-- decreases the value each iteration
This makes the pattern start large and shrink

First Inner Loop

(Spaces):

Condition: j <= rows - i
When i = rows: rows - i = 0 → no spaces
When i = 1: rows - i = rows - 1 → maximum spaces
Spaces increase as we go down

Second Inner Loop

(Stars):

Condition: j <= 2 * i - 1
When i = rows: Maximum stars (top row)
When i = 1: Minimum stars (bottom row)
Stars decrease as we go down

---

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:

Creating diamond patterns (combine full + inverted)
Menu designs
Text formatting
Game graphics (pyramid destruction, shrinking effects)
Data visualization (decreasing trends)

---

Summary

The outer loop counts down from rows to 1 using i--.
The first inner loop prints (rows - i) spaces (increases as we go down).
The second inner loop prints (2 * i - 1) stars (decreases as we go down).
After each row, endl moves to the next line.
This creates an inverted pyramid that starts large at the top and shrinks to a point at the bottom.

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.

Table of Contents