Pyramid Star Pattern in C++

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:
*
***
*****
*******
*********Complete C++ Program
Fixed rows = 5 version:
#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
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.
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.
Odd-width star block
for (j = 1; j <= 2 * i - 1; ++j) cout << "*"; prints 1, 3, 5, …, 2*rows-1 stars for symmetric growth.
Newline
cout << "\n" ends the row. Row length is (rows - i) + (2i - 1) = rows + i - 1 characters.
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.
Variation — User Input Version
Accept rows with cin:
#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 > 0after 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 * iinstead of2 * i - 1(even widths break the classic centered look) - Using
j < rows - iwhen 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
Row i: (rows - i) spaces, then (2i - 1) stars.
2i - 1 forces odd widths so the pyramid stays symmetric.
Same outer row index i as many triangle programs; star formula is what changes the shape.
Time complexity O(n²) for n rows.
Reverse the outer loop to flip the pyramid upside down without changing the inner loop logic.
❓ Frequently Asked Questions
i stars would only add one star per row on one side and would not match the usual centered pyramid.rows - i spaces before the stars shifts the block left as i grows, keeping the middle of each odd-length star run aligned.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.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++.
If you print this pyramid and then print Program 6, you get a filled diamond (Program 10).
13 people found this page helpful
