Inverted Right-Aligned Right-Angled Triangle Star Pattern in C

What You'll Learn
How to print an inverted right-aligned triangle: like Program 3, stars line up on the right, but each row has fewer stars than the one above (5, 4, 3, 2, 1 when rows = 5). You will use one loop for growing indentation (i - 1) spaces and another for the star run k from i to rows.
This is the natural partner to Program 3—same alignment, opposite direction for star counts.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see:
*****
****
***
**
*Complete C Program
Complete C program for the inverted right-aligned triangle:
#include <stdio.h>
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; ++i) {
for (j = 1; j < i; ++j) {
printf(" ");
}
for (k = i; k <= rows; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}🧠 How It Works
Declare Variables
rows is the base width. i indexes rows; j prints spaces; k prints stars.
Outer Loop (Rows)
for (i = 1; i <= rows; ++i) walks from the wide top row down to a single star.
Inner Loop — Spaces
for (j = 1; j < i; ++j) prints i - 1 spaces. Row 1 has none; each later row shifts the stars one column right.
Inner Loop — Stars
for (k = i; k <= rows; ++k) prints rows - i + 1 stars, shrinking as i grows.
New Line
printf("\n") after both inner loops completes the row. Character count per row: (i - 1) + (rows - i + 1) = rows, so width stays constant and the right edge aligns.
Inverted, still right-aligned
Spaces grow while the star run shortens; the right column stays straight. O(rows) prints per row, O(rows²) total, O(1) extra space. Long lines stay readable via horizontal scroll in the result card.
Variation — User Input Version
Read rows with scanf():
#include <stdio.h>
int main() {
int rows;
int i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j < i; ++j) {
printf(" ");
}
for (k = i; k <= rows; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows > 0after input - Rewrite the star loop as
for (k = 1; k <= rows - i + 1; ++k)and compare readability - Switch to Program 3’s star loop
1..ito get the non-inverted right-aligned triangle - Print a right-aligned number ladder (54321, 5432, …)
- Combine with Program 2 mentally: both shrink stars per row; Program 4 adds spaces
Avoid
- Using
j <= iwhen you meantj < ifor spaces (extra space breaks alignment) - Using
kfrom1toihere—that is Program 3’s growing triangle, not this pattern - Forgetting the newline after each row
- Assuming
scanfalways succeeds without checking the return value - Mixing tabs and spaces in the output
Key Takeaways
Row i: print i - 1 spaces, then rows - i + 1 stars.
The star loop k = i .. rows counts exactly those stars in one pass.
Program 3 grows stars; Program 4 shrinks them—same right column, different profile.
Time complexity is O(n²) for n rows.
Master Programs 1–4 and you have left, inverted, right-aligned, and inverted right-aligned triangles.
❓ Frequently Asked Questions
i - 1 spaces, then run k from i to rows for stars. Each row is shorter on the left side of the star block but still ends at the same column, so it looks inverted and right-aligned.rows - i + 1, which is exactly how many stars belong on row i. You can rewrite it as k from 1 to rows - i + 1 if you prefer.rows - i and stars 1..i. Program 4 uses spaces 1..i-1 and stars from i to rows. Same alignment idea; inverted star counts.n rows: each row does Θ(n) character prints.Explore More C Star Patterns!
Pyramids, diamonds, and hollow shapes—all follow from mixing spaces, stars, and loop bounds.
Program 4 is Program 3 with the star-count rule flipped: widest row on top instead of bottom. If you can read one version, you can read the other by tracking how i maps to spaces and star length.
12 people found this page helpful
