0
0
CppProgramBeginner · 2 min read

C++ Program to Print Inverted Pyramid Pattern

You can print an inverted pyramid in C++ using nested for loops where the outer loop controls the rows and the inner loops print spaces and stars; for example, use for (int i = n; i >= 1; i--) to print decreasing stars each row.
📋

Examples

Input5
Output***** **** *** ** *
Input3
Output*** ** *
Input1
Output*
🧠

How to Think About It

To print an inverted pyramid, start from the full width of stars on the first line and reduce the number of stars by one each line. Add spaces before the stars to keep the shape centered or aligned to the right. Use one loop to count down the rows and nested loops to print spaces and stars accordingly.
📐

Algorithm

1
Get the number of rows (n) from the user
2
For each row from n down to 1:
3
Print spaces equal to (n - current row number)
4
Print stars equal to the current row number
5
Move to the next line
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows: ";
    cin >> n;

    for (int i = n; i >= 1; i--) {
        for (int space = 0; space < n - i; space++)
            cout << ' ';
        for (int star = 0; star < i; star++)
            cout << '*';
        cout << '\n';
    }
    return 0;
}
Output
Enter number of rows: 5 ***** **** *** ** *
🔍

Dry Run

Let's trace the program with input 3 through the code

1

Input

User enters n = 3

2

First row (i=3)

Print 0 spaces, then 3 stars: ***

3

Second row (i=2)

Print 1 space, then 2 stars: **

4

Third row (i=1)

Print 2 spaces, then 1 star: *

Row (i)Spaces (n - i)Stars (i)Output line
303***
212 **
121 *
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from n down to 1, controlling how many stars to print each line.

Step 2: Print spaces before stars

The first inner loop prints spaces to shift stars right, using n - i spaces to align the pyramid.

Step 3: Print stars

The second inner loop prints stars equal to the current row number i, decreasing each line to form the inverted pyramid.

🔄

Alternative Approaches

Using while loops
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows: ";
    cin >> n;

    int i = n;
    while (i >= 1) {
        int space = 0;
        while (space < n - i) {
            cout << ' ';
            space++;
        }
        int star = 0;
        while (star < i) {
            cout << '*';
            star++;
        }
        cout << '\n';
        i--;
    }
    return 0;
}
This uses while loops instead of for loops, which some beginners find easier to understand but can be more verbose.
Right-aligned inverted pyramid without spaces
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows: ";
    cin >> n;

    for (int i = n; i >= 1; i--) {
        for (int star = 0; star < i; star++)
            cout << '*';
        cout << '\n';
    }
    return 0;
}
This prints an inverted pyramid aligned to the left without spaces, simpler but less visually centered.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner loops run up to n times, resulting in O(n^2) time.

Space Complexity

Only a few variables are used for counting; no extra space proportional to input size is needed, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using for loops is concise and clear, while while loops are more verbose but equally efficient.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Clear and concise code
While loopsO(n^2)O(1)Beginners learning loop control
No spaces (left-aligned)O(n^2)O(1)Simpler output without alignment
💡
Use nested loops: one for spaces and one for stars to shape the inverted pyramid.
⚠️
Beginners often forget to print spaces before stars, causing the pyramid to be left-aligned instead of inverted and shifted.