Shape Rule
Wide first
First line has rows stars; each next line has one fewer down to 1.

The inverted right-angled triangle is the mirror of Program 1: same inner star loop, but the outer loop counts down so the widest line prints first. This tutorial covers reverse iteration, an equivalent forward-loop formula, a live preview, algorithm steps, worked Java examples, edge cases, and complexity.
Wide first
First line has rows stars; each next line has one fewer down to 1.
Countdown
for (i = rows; i >= 1; i--) starts at the widest row.
Same as P1
for (j = 1; j <= i; j++) still prints exactly i stars.
rows - i + 1
Forward outer loop with star count rows - i + 1 draws the same shape.
1–20 rows
Pick a row count and draw the inverted triangle instantly.
Complexity
Total stars = n(n+1)/2 — same count as Program 1.
An inverted right-angled triangle shrinks by one * on each new line. With the right angle on the left, the console output looks like an upside-down staircase of stars.
Compared with Program 1, you keep the same j from 1 to i star loop, but run the outer loop from rows down to 1 so the first printed line is the widest.
Reverse outer iteration is a tiny change with a big visual payoff. Pairing it with Program 1 is one of the fastest ways to read nested loops fluently.
i runs rows → 1.
Still print i stars with System.out.print.
Countdown i, or forward with rows - i + 1.
Still O(n²) and n(n+1)/2 stars.
In short: for i from rows down to 1, print i stars with System.out.print, then call System.out.println().
Given a positive integer rows, print a left-aligned inverted right-angled triangle of * characters with rows lines.
// First 5 rows (conceptual shape)
// *****
// ****
// ***
// **
// * | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines (typically ≥ 1). First line has this many stars. |
| Printed output | text | Left-aligned rows of *; line with outer index i has i stars. |
for i from rows down to 1:
for j from 1 to i:
print "*" (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
| Countdown outer | i = rows..1, print i stars | Clearest “invert Program 1” story |
| Forward + formula | i = 1..rows, print rows - i + 1 stars | When you prefer ascending counters |
| Goal | Pattern |
|---|---|
| Countdown rows | for (i = rows; i >= 1; i--) |
Print i stars | for (j = 1; j <= i; j++) System.out.print("*"); |
| End the row | System.out.println(); |
| Forward equivalent | for (j = 1; j <= rows - i + 1; j++) |
| One-line row shortcut | System.out.println("*".repeat(i)); while counting down |
Same star counts — different outer-loop direction (or bound).
i = 1..rowsStars grow: *, **, ***, …
i = rows..1Stars shrink: *****, ****, …, *
rows - i + 1Ascending i, decreasing star count
name bothCountdown is clearer; formula shows bound flexibility
Reach for the inverted triangle when teaching reverse outer bounds after Program 1.
Natural second lab: flip one loop, keep the rest.
Practice i-- outer loops with a clear visual check.
Rewrite with rows - i + 1 to separate “row number” from “star count.”
Lower halves of diamonds reuse the same countdown idea.
Terminal teaching pattern — not how you build app screens.
Key benefit: one-line change from Program 1 that locks in how outer-loop direction controls the picture.
Choose a row count between 1 and 20 and draw the inverted triangle in the browser.
Three complete Java programs — countdown outer loop, Scanner input, and a forward-loop equivalent with "*".repeat(). Click View Output to reveal sample console results.
Print five rows by counting the outer loop down from 5.
rows = 5 (Countdown)Classic reverse outer loop — the clearest invert of Program 1.
public class InvertedTriangle {
public static void main(String[] args) {
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
} When i = 5, the inner loop prints five stars. Then i becomes 4, 3, 2, and finally 1 — each time printing fewer stars. println() after the inner loop starts the next (shorter) row.
Let the user choose the height at runtime.
Read rows with Scanner and nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class InvertedTriangleInput {
public static void main(String[] args) {
int rows;
int i, j;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
rows = sc.nextInt();
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
} Same countdown core as Example 1; only the source of rows changes. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Same shape without counting the outer loop backward.
rows - i + 1Ascending i with a decreasing star count; uses "*".repeat for brevity.
public class InvertedTriangleForward {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int stars = rows - i + 1;
System.out.println("*".repeat(stars));
}
}
} When i = 1, stars = 5; when i = 5, stars = 1. Same picture as the countdown version — useful when an interviewer asks for an ascending outer loop.
Create a class with main. Set rows (fixed or from Scanner input). The first line will have rows stars.
for (i = rows; i >= 1; i--) starts at the widest row and counts down.
for (j = 1; j <= i; j++) prints exactly i stars with System.out.print("*").
System.out.println() ends the row before i decreases again.
Total stars: n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 4Trace each outer-loop value of i as it counts down, and count how many times the inner loop runs.
i | Inner j range | Printed row | Stars this row |
|---|---|---|---|
4 | 1..4 | **** | 4 |
3 | 1..3 | *** | 3 |
2 | 1..2 | ** | 2 |
1 | 1..1 | * | 1 |
Total star prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same total as the upright triangle of height 4.
Where this inverted pattern (and reverse outer loops) shows up beyond the homework prompt.
Show how one bound change flips the picture.
Example: side-by-side outputs for rows = 5.
Countdown outer loops appear again in filled diamonds.
Example: Program 10 lower phase.
Practice expressing star count as a formula of i.
Example: stars = rows - i + 1.
Swap * for digits once the countdown works.
Example: print i instead of *.
Same triangular total as the upright triangle.
Example: count stars for n = 10 → 55.
Pair with hasNextInt() and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: say “same inner loop as Program 1, outer loop reversed” before coding — that is the whole design.
Why this inverted pattern is a perfect second exercise.
Change one loop header and the picture flips — great for learning.
Wrong outer direction or bound shows up as the upright triangle.
Countdown or forward formula — both are interview-friendly.
Streaming output needs no storage beyond loop counters.
Pro Tip: lead with the countdown story for clarity, then mention the rows - i + 1 rewrite as a follow-up.
Small habits that keep inverted-triangle code clean.
rows, Not a LiteralWrite i = rows so changing the height does not require editing the loop header twice.
Only call System.out.println() after the inner star loop finishes.
hasNextInt()Avoid crashes when the user types letters instead of a number.
Countdown and rows - i + 1 — pick one, mention the other.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if the output grows like Program 1, your outer loop is still counting up — flip it or switch the star bound.
Mistakes that commonly break inverted star triangles.
You reprint Program 1 instead of the inverted shape.
→ Use for (i = rows; i >= 1; i--) or change the star bound.
Each star lands on its own line — a column, not a triangle.
→ Use System.out.print for stars; System.out.println() only after the inner loop.
5 in the LoopChanging rows no longer updates the outer bound.
→ Always start from the rows variable.
Using rows - i instead of rows - i + 1 drops the last star on each line.
→ First forward row needs rows stars: rows - 1 + 1.
Letters or empty input throw InputMismatchException.
→ Check hasNextInt() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just * — same as Program 1 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; noisy for huge n.
nextInt() throws — check hasNextInt().
i == rowsRemember: the first printed line uses the largest i, not index 1.
Try these variations to lock in the pattern.
rows - i + 1 starshasNextInt() until rows >= 1n(n+1)/2 — only print order changes.rows > 0 for interactive programs; rows = 1 prints a single star.Quick Takeaway: outer loop counts down from rows, inner loop prints i stars, then break the line — that is the inverted triangle.
| Program | Time | Extra space |
|---|---|---|
| Countdown nested loops (Examples 1–2) | O(rows²) | O(1) |
Forward + "*".repeat (Example 3) | O(rows²) | O(rows) temporary per row string |
The inverted right-angled triangle is Program 1 with a reversed outer loop: widest line first, then one fewer star each row. Master the countdown version, then know the rows - i + 1 rewrite for ascending counters.
Practice the three examples above, then continue to the right-aligned triangle for leading spaces.
Outer loop rows → 1, inner loop prints i stars — keep print/println separated, and validate row counts when reading input.
rows, not a hard-coded numberSystem.out.print for stars and System.out.println() after each rowrows - i + 1 alternate formulationSystem.out.println() inside the inner star looprows - i when you meant rows - i + 1rows = 1 edge casePrint the upside-down triangle the beginner-friendly way.
Wide line first
Definitionrows → 1
CodeSame as Program 1
Coderows - i + 1
OptionO(n²) time
AnalysisThis inverted triangle uses the same inner loop as Program 1 — only the outer loop direction changes. Total stars stay n(n+1)/2, so complexity is still O(n²).
Add a leading-space loop so the right angle sits on the right edge.
12 people found this page helpful