Inverted Pyramid Star Pattern in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

What You'll Learn

This program prints an inverted pyramid by decreasing the odd number of stars row by row, while increasing leading spaces to keep the pyramid centered.

Star count per row is 2 * i - 1 (with i decreasing from rows to 1).

⭐ Pattern Output

When you run the program with rows = 5:

Output
*********
 *******
  *****
   ***
    *
1

Complete C++ Program

Fixed rows = 5 version:

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    int i, j;

    for (i = rows; i >= 1; --i) {
        for (j = 1; j <= rows - i; ++j) cout << " ";
        for (j = 1; j <= 2 * i - 1; ++j) cout << "*";
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Outer loop: base to tip

for (i = rows; i >= 1; --i) prints the wide row first. Inner logic matches the upright pyramid; only the outer direction flips.

Outer
2

Leading spaces

for (j = 1; j <= rows - i; ++j) cout << " "; — as i shrinks, rows - i grows, indenting narrower rows more.

Align
3

Odd stars, shrinking

for (j = 1; j <= 2 * i - 1; ++j) cout << "*"; removes two stars per row as i steps down.

Stars
4

Newline

cout << "\n" after each pair of inner loops completes the centered line.

Newline
=

Inverted centered pyramid

O(rows²) character writes, O(1) extra space. Long middle rows scroll sideways in the mobile-friendly result strip.

2

Variation — User Input Version

Accept rows with cin:

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    int i, j;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = rows; i >= 1; --i) {
        for (j = 1; j <= rows - i; ++j) cout << " ";
        for (j = 1; j <= 2 * i - 1; ++j) cout << "*";
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Flip back to Program 5 by changing the outer loop to 1..rows
  • Validate rows > 0 after cin
  • Draw a hollow inverted pyramid (outline only)
  • Use digits instead of * for an inverted number pyramid
  • Compare mentally with Program 2 (inverted left triangle without centering)

Avoid

  • Changing the star bound to 2 * i (even widths break the usual symmetric look)
  • Using i++ on the outer loop by mistake (you need to decrement)
  • Altering the space formula without checking alignment
  • Omitting cout << "\n" or endl between rows
  • Ignoring invalid input or negative rows after cin

Key Takeaways

1

Same inner loops as Program 5; reverse the outer loop to invert the pyramid.

2

As i decreases, rows - i grows and 2i - 1 shrinks—wider margin, fewer stars.

3

Printed star counts for rows = 5: 9, 7, 5, 3, 1.

4

Time complexity O(n²) for n rows.

5

This is the centered analogue of “invert the outer loop” from Program 1 → Program 2.

❓ Frequently Asked Questions

With for (i = rows; i >= 1; --i), the first iteration uses the largest i, so you print the longest line first. Later iterations use smaller i, so 2*i-1 falls while rows-i rises.
rows - i grows when i gets smaller. That extra left padding keeps shorter star runs centered under the first (widest) row.
Program 5 increments i from 1 to rows (tip to base). Program 6 decrements i from rows to 1 (base to tip). Inner loops are unchanged.
O(n²) for n rows: same per-row print totals as Program 5, only iteration order differs.

Next: Inverted V Hollow Pattern

Continue to Program 7 to print an inverted V-shaped hollow star pattern in C++.

Program 7 →
Did you know?

If you print an upright pyramid (Program 5) followed by this inverted pyramid, you get the filled diamond shape (Program 10).

About the author

Mari Selvan M P
Mari Selvan M P šŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful