Shape Rule
Peak on top
One star at the tip; each row adds one star on both sides.

A center-aligned pyramid combines leading spaces with odd star counts: (rows - i) spaces and (2 * i - 1) stars on row i. This tutorial covers the centering formula, why odd widths matter, a live preview, algorithm steps, worked C++ examples, edge cases, and complexity.
Peak on top
One star at the tip; each row adds one star on both sides.
rows - i
Same centering margin as Program 3 — shrinks as rows widen.
2*i - 1
Print 1, 3, 5, … stars so the pyramid stays symmetric.
2n - 1
Bottom row has 2 * rows - 1 stars and no leading spaces.
1–14 rows
Pick a height and draw the centered pyramid instantly.
n² stars
Sum of first n odd numbers is n² — O(n²) time, O(1) extra space.
A center-aligned pyramid starts with a single * and widens by two stars each row, with leading spaces so the peak stays centered.
It extends Program 3’s spacing idea, but uses 2 * i - 1 stars instead of i. The same row body is the upper half of the filled diamond.
Odd-width centering is the key skill behind pyramids, diamonds, and many hollow variants. Once 2*i-1 clicks, those patterns become small variations.
2*i - 1 keeps a single center column.
rows - i centers each star run.
Spaces first, then the odd star run.
Upper half of Program 10’s filled diamond.
In short: for each row i, print rows - i spaces, then 2 * i - 1 stars, then a newline.
Given a positive integer rows, print a center-aligned full pyramid of * characters with rows lines.
// First 5 rows (spaces shown as ·)
// ····*
// ···***
// ··*****
// ·*******
// ********* | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height (typically ≥ 1). Base width is 2 * rows - 1. |
| Printed output | text | Centered rows: (rows - i) spaces + (2 * i - 1) stars. |
for i from 1 to rows:
for j from 1 to (rows - i):
print " "
for k from 1 to (2 * i - 1):
print "*"
print newline | Approach | Idea | Best for |
|---|---|---|
| Two nested loops | Spaces then odd star run | Learning and interviews |
string() shortcut | Build padding and stars as strings | Shorter demos after formulas click |
| Goal | Pattern |
|---|---|
| Walk each row | for (i = 1; i <= rows; i++) |
| Leading spaces | for (j = 1; j <= rows - i; j++) cout << " "; |
| Odd star run | for (k = 1; k <= 2 * i - 1; k++) cout << "*"; |
| Base width | 2 * rows - 1 |
| Invert later | for (i = rows; i >= 1; i--) (see Program 6) |
| String shortcut | cout << string(rows - i, ' ') << string(2 * i - 1, '*') << "\n"; |
Same space idea — star formula defines the shape.
i starsRight-aligned triangle — same spaces
2*i - 1Centered pyramid — odd star runs
countdown iInverted pyramid — same inners
+ mirrorFilled diamond — this page as upper half
Reach for a centered pyramid when teaching odd-width growth after right-aligned triangles.
Natural step once Programs 1–4 are solid.
2*i-1 is a classic loop bound interview warm-up.
Filled diamonds reuse this exact upper-half body.
Fixed-pitch fonts make centering mistakes obvious.
Terminal teaching pattern — not how you build app screens.
Key benefit: one figure that locks in centering spaces and odd-width growth — the gateway to diamonds.
Choose a height between 1 and 14 and draw the centered pyramid in the browser.
Three complete C++ programs — nested space/star loops, console input, and a string() shortcut. Click View Output to reveal sample console results.
Print a five-row centered pyramid with classic nested loops.
rows = 5Space loop with rows - i, star loop with 2 * i - 1.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
cout << " ";
}
for (k = 1; k <= 2 * i - 1; ++k) {
cout << "*";
}
cout << "\n";
}
return 0;
} When i = 1, print 4 spaces and 1 star. When i = 5, print 0 spaces and 9 stars (2*5-1). Each step adds one star on the left and one on the right of the previous run.
Let the user choose the height at runtime.
Read rows with cin >> rows (check cin.fail() in real apps).
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
cout << " ";
}
for (k = 1; k <= 2 * i - 1; ++k) {
cout << "*";
}
cout << "\n";
}
return 0;
} Same space/star core as Example 1; only the source of rows changes. Non-numeric input sets cin’s fail bit if you ignore errors — always validate in safer labs.
Same pyramid without explicit character loops.
string() for Spaces and StarsBuild each row’s margin and odd star run in one call each.
#include <iostream>
#include <string>
using namespace std;
int main() {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
cout << string(rows - i, ' ') << string(2 * i - 1, '*') << "\n";
}
return 0;
} Same formulas as Example 1; string() replaces the two inner loops. Keep the nested-loop version for exams that want both bounds visible.
Set rows. Use i for the row, j for spaces, k for stars.
for (i = 1; i <= rows; i++) — tip when i == 1, base when i == rows.
for (j = 1; j <= rows - i; j++) cout << " "; shrinks the margin as the row widens.
for (k = 1; k <= 2 * i - 1; k++) cout << "*"; then cout << "\n".
Total stars n²; O(n²) time, O(1) extra space. Base width 2n - 1.
rows = 4Trace spaces, stars, and characters per row for each outer-loop value of i.
i | Spaces rows - i | Stars 2*i - 1 | Chars before newline | Printed row |
|---|---|---|---|---|
1 | 3 | 1 | 4 | * |
2 | 2 | 3 | 5 | *** |
3 | 1 | 5 | 6 | ***** |
4 | 0 | 7 | 7 | ******* |
Star total: 1+3+5+7 = 16 = 4². Tip rows are shorter than the base — unlike Program 3’s fixed width.
Where this pyramid (and odd-width centering) shows up beyond the homework prompt.
2*i-1 is a clear visual of the first n odd numbers.
Example: assert sum of stars equals rows * rows.
Reuse this body, then mirror from rows - 1.
Example: Program 10.
Countdown outer loop flips the pyramid.
Example: Program 6.
Same spaces; swap i stars for 2*i-1.
Example: side-by-side for rows = 5.
Once solid works, print border stars only.
Example: stars on edges of each odd run.
Pair with a cin validation check and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: say “rows - i spaces and 2*i-1 stars” before writing a single loop — that is the whole design.
Why the centered pyramid is a favorite mid-series pattern.
Reuses Program 3’s margin; only the star formula is new.
Even widths or wrong spaces look “off” immediately.
Same row body powers filled and hollow diamond halves.
Total stars = n² makes O(n²) concrete and memorable.
Pro Tip: master the nested-loop version first; treat string() as a polish shortcut afterward.
Small habits that keep pyramid code clean.
2 * i - 12 * i (even) breaks the classic single-peak look.
Always print the margin first — order matters for centering.
cin.fail()Avoid crashes when the user types letters instead of a number.
Tabs break centering across fonts and editors.
Trace rows = 4 on paper before coding larger demos.
Pro Tip: if the shape leans like a right triangle, you almost certainly used i stars instead of 2*i-1.
Mistakes that commonly break centered pyramids.
i Stars Instead of 2*i - 1You get a right-aligned triangle look, not a balanced pyramid.
→ Keep odd counts: 2 * i - 1.
2 * i (Even Width)Even lengths lose the single center peak of the classic shape.
→ Prefer 2 * i - 1.
j < rows - i instead of <= drops a needed space and shifts the peak.
→ Use j <= rows - i.
Centering looks fine in one editor and broken in another.
→ Always print the space character " ".
Letters or empty input throw failed cin state.
→ Check cin.fail() and re-prompt on failure.
Check these inputs before calling the solution done.
0 spaces + 1 star — the tip is the whole pyramid.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Base width 2n-1 — fine for labs; may wrap on tiny terminals.
cin throws — use cin validation.
i == rowsSpace loop runs 0 times; print 2*rows-1 stars only.
Try these variations to lock in the pattern.
2*i-1 to irows down to 1rows * rowsrows - 1 down to 1(rows - i) + (2i - 1) = rows + i - 1 — tip rows are shorter than the base.rows > 0 for interactive programs; rows = 1 prints a single star.Quick Takeaway: print rows - i spaces, then 2 * i - 1 stars — that is the centered pyramid.
| Program | Time | Extra space |
|---|---|---|
| Nested space/star loops (Examples 1–2) | O(rows²) | O(1) |
string() shortcut (Example 3) | O(rows²) | O(rows) temporary per row string |
Total stars = rows²; each row also prints up to Θ(rows) spaces.
The center-aligned pyramid is leading spaces plus odd star counts: rows - i spaces and 2 * i - 1 stars. Master that pair and inverted pyramids or filled diamonds become small follow-ups.
Practice the three examples above, then continue to the inverted centered pyramid.
Spaces shrink, odd stars grow, total stars = n² — keep 2*i-1, and validate row counts when reading input.
rows - i spaces and 2*i-1 stars before codingcin.fail() for interactive demosi stars when you meant a centered pyramid2 * i even widths for the classic shaperows = 1 edge casePrint the full pyramid the beginner-friendly way.
Spaces + odd stars
Definitionrows - i
Formula2*i - 1
Formulan² stars
MathO(n²) time
AnalysisOdd star counts 1, 3, 5, … come from 2 * i - 1. Their sum for n rows is n² — so total stars grow as a perfect square. This pyramid is also the upper half of the filled diamond.
Keep the same inner loops and count the outer loop down for an upside-down pyramid.
12 people found this page helpful