Shape Rule
Base on top
Widest odd star run first; tip star lands on the last line.

An inverted centered pyramid reuses Program 5’s formulas — (rows - i) spaces and (2 * i - 1) stars — but runs the outer loop from rows down to 1 so the widest row prints first. This tutorial covers reverse iteration, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Base on top
Widest odd star run first; tip star lands on the last line.
i = rows..1
Reverse the outer loop — that is the only change from Program 5.
spaces + stars
rows - i spaces then 2*i - 1 stars — unchanged bodies.
9, 7, 5…
For five rows, printed star counts fall by two each line.
1–14 rows
Pick a height and draw the inverted pyramid instantly.
n² stars
Same totals as Program 5 — O(n²) time, O(1) extra space.
An inverted center-aligned pyramid starts with the base row and narrows to a single tip star, with leading spaces so each shorter run stays centered.
It is the flip of Program 5: keep the same space and star formulas, reverse only the outer loop. That mirrors how Program 2 inverts Program 1, but with odd-width centering. The same body is the lower half of the filled diamond.
Countdown outer loops are a classic interview tweak. Once you see that inverting a pyramid is “same inners, reverse i,” diamonds and stacked shapes become simple composition.
i from rows down to 1.
rows - i rises as i falls.
2*i - 1 steps down by odds.
Lower half of Program 10’s filled diamond.
In short: for i from rows down to 1, print rows - i spaces, then 2 * i - 1 stars, then a newline.
Given a positive integer rows, print an inverted center-aligned pyramid of * characters with rows lines (widest first).
# First 5 rows (spaces shown as ·)
# *********
# ·*******
# ··*****
# ···***
# ····* | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height (typically ≥ 1). First line width is 2 * rows - 1. |
| Printed output | text | Base-to-tip rows: (rows - i) spaces + (2 * i - 1) stars with countdown i. |
for i from rows down to 1:
for j from 1 to (rows - i):
print " "
for k from 1 to (2 * i - 1):
print "*"
print newline | Approach | Idea | Best for |
|---|---|---|
| Countdown + two inners | Same as Program 5; reverse outer | Learning and interviews |
"*" * shortcut | Build padding and stars as strings | Shorter demos after formulas click |
| Goal | Pattern |
|---|---|
| Walk rows base → tip | for i in range(rows, 0, -1): |
| Leading spaces | for j in range(1, rows - i + 1): print(" ", end="") |
| Odd star run | for k in range(1, 2 * i): print("*", end="") |
| First-line width | 2 * rows - 1 stars, 0 spaces |
| Flip upright | for i in range(1, rows + 1): (see Program 5) |
| String shortcut | print(" " * (rows - i) + "*" * (2 * i - 1)) |
Same space/star formulas — outer-loop direction defines upright vs inverted.
i = 1..rowsUpright pyramid — tip first
i = rows..1Inverted pyramid — base first
i starsInverted left triangle — no centering
+ upperFilled diamond — this page as lower half
Reach for an inverted pyramid when teaching countdown loops after the upright centered pyramid.
Natural “change one loop” follow-up once upright pyramids click.
for i in range(n, 0, -1): is a staple interview warm-up.
Filled diamonds print this shape under the upright pyramid.
Same “invert by reversing i” idea, with centering this time.
Console teaching pattern — not how you build app screens.
Key benefit: proves that flipping a centered pyramid is one outer-loop change — the gateway to stacking diamond halves.
Choose a height between 1 and 14 and draw the inverted centered pyramid in the browser.
Three complete Python programs — countdown nested loops, console input, and a "*" * shortcut. Click View Output to reveal sample console results.
Print a five-row inverted centered pyramid with classic nested loops.
rows = 5Outer loop counts down; space loop uses rows - i; star loop uses 2 * i - 1.
rows = 5
for i in range(rows, 0, -1):
for j in range(1, rows - i + 1):
print(" ", end="")
for k in range(1, 2 * i):
print("*", end="")
print() When i = 5, print 0 spaces and 9 stars. When i = 1, print 4 spaces and 1 star. Star counts per printed line: 9, 7, 5, 3, 1.
Let the user choose the height at runtime.
Read rows with input() and int() (wrap in try/except ValueError in real apps).
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
for j in range(1, rows - i + 1):
print(" ", end="")
for k in range(1, 2 * i):
print("*", end="")
print() Same countdown space/star core as Example 1; only the source of rows changes. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Same inverted pyramid without explicit character loops.
" " * and "*" * MultiplicationBuild each row’s margin and odd star run in one call each, still counting down.
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1)) Same formulas as Example 1; "*" * replaces the two inner loops. Keep the nested-loop version for exams that want both bounds visible.
Set rows. Use i counting down, j for spaces, k for stars.
for i in range(rows, 0, -1): — base when i == rows, tip when i == 1.
for j in range(1, rows - i + 1): print(" ", end="") — margin grows as i shrinks.
for k in range(1, 2 * i): print("*", end="") then print().
Total stars still n²; O(n²) time, O(1) extra space. First line width 2n - 1.
rows = 4Trace spaces, stars, and characters per row as i counts down from 4 to 1.
i | Spaces rows - i | Stars 2*i - 1 | Chars before newline | Printed row |
|---|---|---|---|---|
4 | 0 | 7 | 7 | ******* |
3 | 1 | 5 | 6 | ***** |
2 | 2 | 3 | 5 | *** |
1 | 3 | 1 | 4 | * |
Star total: 7+5+3+1 = 16 = 4² — same as Program 5, different print order.
Where this inverted pyramid (and countdown centering) shows up beyond the homework prompt.
One formula pair, two shapes — upright vs inverted.
Example: flip Program 5’s outer loop only.
Stack under Program 5 (often from rows - 1).
Example: Program 10.
Change to i = 1..rows to restore Program 5.
Example: Program 5.
Same invert idea; Program 2 has no leading spaces.
Example: side-by-side for rows = 5.
Once solid works, print border stars only.
Example: outline of each odd run.
Pair with input validation and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: say “Program 5 inners, countdown outer” before coding — that is the whole design.
Why the inverted pyramid is a favorite follow-up pattern.
Reuse known space/star formulas; only reverse i.
Wrong direction instantly prints an upright pyramid instead.
Pair with Program 5 for filled diamonds.
Total stars still n² — order does not change big-O.
Pro Tip: master the nested-loop countdown first; treat "*" * as a polish shortcut afterward.
Small habits that keep inverted-pyramid code clean.
rows, Step Downi++ by mistake reprints Program 5.
2 * i - 1Even widths break the classic single-peak tip.
int(input()) in try/exceptAvoid crashes when the user types letters instead of a number.
Tabs break centering across fonts and editors.
Trace rows = 4 countdown on paper before larger demos.
Pro Tip: if the tip prints first, you almost certainly used i++ instead of i--.
Mistakes that commonly break inverted pyramids.
for i in range(1, rows + 1): reprints the upright pyramid.
→ Use for i in range(rows, 0, -1):.
i Stars Instead of 2*i - 1You lose the centered odd-width shape.
→ Keep odd counts: 2 * i - 1.
j < rows - i instead of <= shifts the tip off-center.
→ Use j <= rows - i.
Centering looks fine in one editor and broken in another.
→ Always print the space character " ".
Letters or empty input throw ValueError.
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
One iteration: 0 spaces + 1 star — tip and base coincide.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Top width 2n-1 — fine for labs; may wrap on tiny terminals.
int(input()) raises ValueError — validate first.
i == rowsSpace loop runs 0 times; print 2*rows-1 stars only.
Try these variations to lock in the pattern.
i = 1..rows2*i-1rows * rowsrows - 1rows + i - 1 — tip rows are shorter than the top base.rows > 0 for interactive programs; rows = 1 prints a single star.Quick Takeaway: countdown i from rows to 1, print rows - i spaces and 2 * i - 1 stars — that is the inverted pyramid.
| Program | Time | Extra space |
|---|---|---|
| Nested space/star loops (Examples 1–2) | O(rows²) | O(1) |
"*" * shortcut (Example 3) | O(rows²) | O(rows) temporary per row string |
Total stars = rows²; each row also prints up to Θ(rows) spaces. Same as Program 5.
The inverted centered pyramid is Program 5 with a countdown outer loop: rows - i spaces and 2 * i - 1 stars, printed from base to tip. Master that flip and diamond halves become a short stacking exercise.
Practice the three examples above, then continue to the hollow inverted-V pattern.
Spaces grow, odd stars shrink, total stars = n² — keep i--, and validate row counts when reading input.
try/except ValueError for interactive demosi when you meant an inverted pyramid2 * i even widths for the classic shaperows = 1 edge casePrint the upside-down pyramid the beginner-friendly way.
Countdown + odd stars
Definitioni = rows..1
DirectionSame as Prog 5
Formulan² stars
MathO(n²) time
AnalysisThis inverted pyramid is exactly Program 5 with the outer loop reversed — the same relationship as Program 1 versus Program 2, but with centered odd-width rows. Total stars still equal n²; only print order changes.
Next up: a hollow inverted-V outline that becomes the upper half of a hollow diamond.
12 people found this page helpful