An inverted right-angled triangle star pattern prints a left-aligned upside-down staircase of * characters: the first row has rows stars, and each next row has one fewer down to 1.
Remember
Rule: on outer value i, print i stars (i counts down)
*****
****
***
**
* ← 5 rows
In C you solve it with two nested for loops: the outer loop counts from rows down to 1, the inner loop prints stars for the current i, then printf("\n") moves to the next line. It is the mirror of Program 1 — same inner loop, reversed outer direction.
Approach
How to Solve It
Two ways to emit the same shape — start with a countdown outer loop, then optionally use a forward formula.
Method
Idea
Best for
Countdown outer
i = rows..1, inner prints i stars via printf("*")
Learning, interviews, clearest invert of Program 1
Forward + formula
i = 1..rows, print rows - i + 1 stars
When you prefer ascending counters
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
print "*" (no newline)
print newline
Change the row count and the inverted triangle updates instantly — including the triangular star total.
Whole numbers from 1 to 20. Tap a chip or type a value — the first line has that many stars.
Live result5 rows · 15 stars
*****
****
***
**
*
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i as it counts down, and count how many times the inner loop runs.
i
Inner j
Printed row
Stars
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 triangular sum as Program 1 — time is still O(n²).
Code
C Programs
Three complete programs: fixed rows (countdown), console input, and a forward-loop formula. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height with a countdown outer loop — the clearest invert of Program 1.
c
#include <stdio.h>
int main(void) {
int rows = 5;
int i, j;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("*");
}
printf("\n");
}
return 0;
}
Output
*****
****
***
**
*
How It Works
1. Set height.rows = 5 means the triangle has five lines; the first line has five stars.
2. Outer loop counts down.i runs from rows down to 1.
3. Inner loop prints stars. For each i, j runs from 1 to i, so the current row gets exactly i stars via printf("*").
4. Break the line.printf("\n") after the inner loop starts the next (shorter) row.
When i = 5 you get *****; when i = 4 you get ****; and so on down to one star.
Example 2 — User Input Version
Read the row count at runtime. Prefer checking scanf’s return value (shown in the tip below).
c
#include <stdio.h>
int main(void) {
int rows;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("*");
}
printf("\n");
}
return 0;
}
Output (when user enters 7)
Enter the number of rows: 7
*******
******
*****
****
***
**
*
How It Works
1. Prompt and read. Ask for a row count, then fill rows with scanf.
2. Same nested-loop core. Only the source of rows changes — the countdown print logic matches Example 1.
3. Safer input tip. Ignoring scanf’s return leaves rows unset on bad input. Prefer:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1) {
printf("Enter a positive whole number.\n");
return 1;
}
Example 3 — Forward Loop + rows - i + 1
Same inverted shape without counting the outer loop down — star count is a formula of the row number.
c
#include <stdio.h>
int main(void) {
int rows = 5;
int i, j;
for (i = 1; i <= rows; ++i) {
int stars = rows - i + 1;
for (j = 1; j <= stars; ++j) {
printf("*");
}
printf("\n");
}
return 0;
}
Output
*****
****
***
**
*
How It Works
1. Outer loop counts up.i still runs from 1 to rows, like Program 1.
2. Star count shrinks by formula. Row i prints rows - i + 1 stars — so row 1 gets rows stars, and the last row gets 1.
3. Same print and newline. Inner printf("*") and trailing printf("\n") are unchanged.
Learn the countdown version first (Examples 1–2) so the invert of Program 1 is obvious; treat this as an alternate wording of the same idea.
Edge Cases & Pitfalls
Check these before calling the solution done.
Wrong direction
Upright instead of inverted
If you use i = 1..rows with j <= i, you get Program 1. For the inverted shape, count down — or use rows - i + 1 stars with a forward loop.
Newline inside
Column of stars
If printf("\n") is inside the inner loop, each star lands on its own line. Print stars without a newline; break the line only after the inner loop.
j <= rows
Rectangle, not triangle
Inner bound must be j <= i (or j <= rows - i + 1). j <= rows prints a filled rectangle.
No newline
One endless line
Omitting the row break glues every star onto a single line.
rows = 1
Single star
Output is just * on one line — a good sanity check (same as Program 1).
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad scanf
Check the return
If scanf fails, rows is uninitialized — always test != 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Countdown nested loops (Examples 1–2)
O(rows²)
O(1)
Forward + rows - i + 1 (Example 3)
O(rows²)
O(1)
Total stars printed = n + (n-1) + … + 1 = n(n+1)/2, which is still quadratic in n — identical to Program 1.
Remember
Key Takeaways
Rule: outer value i prints exactly i stars, with i counting from rows down to 1.
Same as Program 1: only the outer loop direction changes; the inner star loop stays j = 1..i.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from the triangular star count; O(1) extra space.
One line: for i from rows down to 1, print i stars with printf("*"), then printf("\n").
Frequently Asked Questions
The outer loop runs i from rows down to 1. For each i, the inner loop prints i stars. The first output line uses i equal to rows so it is the longest; each later line has a smaller i, so the triangle points downward.
Program 1 uses for (i = 1; i <= rows; i++) so stars grow. This program uses for (i = rows; i >= 1; i--) so stars shrink. The inner loop still runs j from 1 to i.
Yes. Use for (i = 1; i <= rows; i++) and print (rows - i + 1) stars in the inner loop. Both styles produce the same shape.
printf("*") stays on the same line. printf("\n") ends the current line. Stars use printf("*"); the row break uses printf("\n") after the inner loop.
O(n²) for n rows. Total stars are still n(n+1)/2, same as the upright triangle.
Yes. putchar('*') writes one character without a format string. Nested loops with putchar are a common, slightly leaner style while i counts down.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
This 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²).