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 a centered pyramid by printing spaces first and then an odd number of stars.

Row i prints rows - i spaces and 2 * i - 1 stars.

⭐ 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 = 1; i <= rows; ++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: tip to base

for (i = 1; i <= rows; ++i) grows the pyramid downward. j is reused for the space loop and the star loop.

Outer
2

Center with spaces

for (j = 1; j <= rows - i; ++j) cout << " "; prints the left margin so the odd star block sits in the middle of the line.

Center
3

Odd-width star block

for (j = 1; j <= 2 * i - 1; ++j) cout << "*"; prints 1, 3, 5, …, 2*rows-1 stars for symmetric growth.

Stars
4

Newline

cout << "\n" ends the row. Row length is (rows - i) + (2i - 1) = rows + i - 1 characters.

Newline
=

Centered pyramid

Same geometry as the upper half of a filled diamond: O(rows²) output, O(1) extra space. The base row scrolls horizontally in the green preview on small screens.

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 = 1; i <= rows; ++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

  • Validate rows > 0 after input
  • Invert the outer loop (i = rows; i >= 1; i--) for an upside-down pyramid
  • Print a hollow pyramid (only borders)
  • Replace * with increasing digits for a number pyramid
  • Compare with Program 3: same space formula rows - i, different star count

Avoid

  • Using 2 * i instead of 2 * i - 1 (even widths break the classic centered look)
  • Using j < rows - i when you need <= (off-by-one spacing)
  • Forgetting the newline after each row
  • Mixing tabs and spaces in output
  • Skipping bounds checks after reading with cin

Key Takeaways

1

Row i: (rows - i) spaces, then (2i - 1) stars.

2

2i - 1 forces odd widths so the pyramid stays symmetric.

3

Same outer row index i as many triangle programs; star formula is what changes the shape.

4

Time complexity O(n²) for n rows.

5

Reverse the outer loop to flip the pyramid upside down without changing the inner loop logic.

❓ Frequently Asked Questions

You need odd lengths 1, 3, 5, … so each row extends by one star on both sides. Using i stars would only add one star per row on one side and would not match the usual centered pyramid.
Printing rows - i spaces before the stars shifts the block left as i grows, keeping the middle of each odd-length star run aligned.
Yes. Use for (i = rows; i >= 1; --i) with the same space and star inner loops: the first line is the widest, then rows narrow toward the tip.
O(n²) for n rows: each row prints Θ(n) characters in the worst case, and there are n rows.

Next: Inverted Pyramid Pattern

Continue to Program 6 to print the inverted pyramid star pattern in C++.

Program 6 →
Did you know?

If you print this pyramid and then print Program 6, you get a filled diamond (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.

13 people found this page helpful