Shape Rule
Wide on top
First line has rows stars; each next line has one fewer, flush right.

The inverted right-aligned triangle keeps a flush right edge like Program 3, but star counts shrink like Program 2: widest line on top. This tutorial covers the space and star formulas, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Wide on top
First line has rows stars; each next line has one fewer, flush right.
i - 1
Print i - 1 spaces (none on row 1) so the block stays right-aligned.
i..rows
k from i to rows prints rows - i + 1 stars.
n chars/row
Every row: (i - 1) + (rows - i + 1) = rows characters.
1–20 rows
Pick a row count and draw the inverted right-aligned triangle instantly.
Complexity
n rows × Θ(n) characters each — O(n²) time, O(1) extra space.
An inverted right-aligned triangle has the widest star run on the first line and shrinks by one star each row, while the right edge stays flush.
It merges Program 2’s decreasing star counts with Program 3’s right alignment: growing spaces (i - 1) plus shrinking stars (rows - i + 1).
It completes the four basic corner triangles (left / inverted / right / inverted-right). After this, centered pyramids are a small jump.
Row i has i - 1 leading spaces.
rows - i + 1 stars (or k = i..rows).
Flush-right like Program 3, inverted like Program 2.
Last of the four basic right-angled variants.
In short: for each row i, print i - 1 spaces, then rows - i + 1 stars, then a newline.
Given a positive integer rows, print an inverted right-aligned right-angled triangle of * characters with rows lines.
# First 5 rows (spaces shown as ·)
# *****
# ·****
# ··***
# ···**
# ····* | Item | Type | Description |
|---|---|---|
rows | int | Number of lines (typically ≥ 1). Also the width of each line. |
| Printed output | text | Right-aligned rows: (i - 1) spaces + (rows - i + 1) stars. |
for i from 1 to rows:
for j from 1 to (i - 1): // range(1, i) in Python
print " "
for k from i to rows: // rows - i + 1 stars
print "*"
print newline | Approach | Idea | Best for |
|---|---|---|
k = i..rows | Star loop bound encodes the count | Matches classic textbook listings |
1..(rows - i + 1) | Explicit star count variable | Clearer when explaining formulas |
| Goal | Pattern |
|---|---|
| Walk each row | for i in range(1, rows + 1): |
| Leading spaces | for j in range(1, i): print(" ", end="") |
| Stars via range | for k in range(i, rows + 1): print("*", end="") |
| Stars via count | for k in range(1, rows - i + 2): |
| Width check | (i - 1) + (rows - i + 1) == rows |
| String shortcut | print(" " * (i - 1) + "*" * (rows - i + 1)) |
Four corner variants — alignment and star growth differ.
left growOnly i stars — no spaces
left shrinkCountdown / rows - i + 1 stars
right growrows - i spaces, i stars
right shrinki - 1 spaces, rows - i + 1 stars
Reach for this figure when combining right alignment with shrinking star counts.
Natural merge of invert + right-align skills.
Spaces grow while stars shrink — opposite trends in one row.
Next: Program 5 centers with odd star counts.
Compare k = i..rows with an explicit star count loop.
Console teaching pattern — not how you build app screens.
Key benefit: one pattern that locks in growing padding and shrinking fill while keeping a fixed right edge.
Choose a row count between 1 and 20 and draw the inverted right-aligned triangle in the browser.
Three complete Python programs — classic k = i..rows loops, console input, and a "*" * shortcut. Click View Output to reveal sample console results.
Print five inverted right-aligned rows with nested loops.
rows = 5Space loop with range(1, i), star loop with k from i to rows.
rows = 5
for i in range(1, rows + 1):
for j in range(1, i):
print(" ", end="")
for k in range(i, rows + 1):
print("*", end="")
print() When i = 1, print 0 spaces and stars for k = 1..5 (five stars). When i = 5, print 4 spaces and stars for k = 5..5 (one star). Every row has exactly 5 characters before the newline.
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(1, rows + 1):
for j in range(1, i):
print(" ", end="")
for k in range(i, rows + 1):
print("*", end="")
print() Same 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 shape with explicit star count and string multiplication.
"*" * + Explicit CountBuild padding and stars from the formulas directly.
rows = 5
for i in range(1, rows + 1):
spaces = i - 1
stars = rows - i + 1
print(" " * spaces + "*" * stars) Naming spaces and stars makes the invert-vs-align story obvious. Keep the k = i..rows version when you want the classic listing style.
Set rows. Use i for the row, j for spaces, k for stars.
for i in range(1, rows + 1): — widest when i == 1, one star when i == rows.
for j in range(1, i): print(" ", end="") prints i - 1 spaces.
for k in range(i, rows + 1): print("*", end="") then print(). Width stays rows.
Star total n(n+1)/2; O(n²) time, O(1) extra space.
rows = 4Trace spaces, stars, and total width for each outer-loop value of i.
i | Spaces i - 1 | Stars rows - i + 1 | k range | Printed row |
|---|---|---|---|---|
1 | 0 | 4 | 1..4 | **** |
2 | 1 | 3 | 2..4 | *** |
3 | 2 | 2 | 3..4 | ** |
4 | 3 | 1 | 4..4 | * |
Check: every row has width 4. Star total: 4+3+2+1 = 10.
Where this inverted right-aligned pattern shows up beyond the homework prompt.
Finish left / invert / right / invert-right before pyramids.
Example: compare all four for rows = 5.
Spaces increase as stars decrease — strong formula practice.
Example: flip only one formula and watch the edge break.
Rewrite k = i..rows as 1..(rows - i + 1).
Example: same output, different loop headers.
Swap to rows - i spaces and 1..i stars.
Example: one edit each way between the two pages.
Next patterns reuse padding plus multi-star runs.
Example: Program 5 uses 2*i - 1 stars.
Pair with input validation and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: say “Program 3 with star growth flipped” before coding — spaces grow, stars shrink.
Why this pattern earns a place after Programs 1–3.
Invert (Program 2) + right-align (Program 3) in one figure.
Every row length equals rows — bugs show up immediately.
Range style or count style — both are interview-friendly.
Streaming output needs only loop counters.
Pro Tip: lead with the formulas spaces = i - 1 and stars = rows - i + 1, then pick a loop style.
Small habits that keep this pattern clean.
range(1, i) for Spacesj <= i adds an extra space and shifts the right edge.
Tabs break alignment across fonts and editors.
int(input()) in try/exceptAvoid crashes when the user types letters instead of a number.
rowsCheck tip and last rows: spaces + stars must sum to rows.
Trace rows = 4 on paper before coding larger demos.
Pro Tip: if the first line has leading spaces, your space formula is wrong — row 1 must use i - 1 = 0 spaces.
Mistakes that commonly break inverted right-aligned triangles.
rows - i spaces and 1..i stars grows instead of shrinks.
→ Use i - 1 spaces and rows - i + 1 stars.
range(1, i + 1) Instead of range(1, i)One extra space per row shifts the right edge leftward.
→ Space loop must run exactly i - 1 times.
1..iThat is Program 3 again — growing, not inverted.
→ Use k = i..rows or count rows - i + 1.
Alignment 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.
0 spaces + 1 star — same as the other triangle pages for n = 1.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
First line has n stars — fine for labs; may wrap on tiny terminals.
int(input()) raises ValueError — validate first.
i == 1Space loop must not run; only a full run of stars.
Try these variations to lock in the pattern.
rows - i spaces and 1..i starsk = i..rows with count rows - i + 1rows >= 1rows: (i - 1) + (rows - i + 1) = rows.rows > 0 for interactive programs; rows = 1 prints a single star.2 * i - 1 stars.Quick Takeaway: print i - 1 spaces, then rows - i + 1 stars — inverted and flush right.
| 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 |
Each of n rows prints Θ(n) characters (spaces + stars).
The inverted right-aligned triangle is Program 3 with star growth flipped: i - 1 spaces and rows - i + 1 stars. With Programs 1–4 you can print any of the four basic right-angled variants.
Practice the three examples above, then continue to the center-aligned pyramid.
Spaces grow, stars shrink, width stays rows — keep range(1, i) for spaces, and validate row counts when reading input.
i - 1 spaces and rows - i + 1 stars before codingrange(1, i) (not range(1, i + 1)) for the space loopk = i..rows and the explicit count formrowstry/except ValueError for interactive demosj <= i1..i when you meant invertedrows = 1 edge casePrint the flush-right inverted triangle the beginner-friendly way.
Wide on top, flush right
Definitioni - 1
Formularows - i + 1
FormulaAlways rows
CheckO(n²) time
AnalysisUse leading spaces and odd star counts (2 * i - 1) to print a full pyramid.
12 people found this page helpful