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

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spaces + decreasing stars

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:

Output
*****
 ****
  ***
   **
    *
1

Complete C Program

Complete C program for the inverted right-aligned triangle:

c
#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

1

Declare Variables

rows is the base width. i indexes rows; j prints spaces; k prints stars.

Setup
2

Outer Loop (Rows)

for (i = 1; i <= rows; ++i) walks from the wide top row down to a single star.

Row control
3

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.

Indent
4

Inner Loop — Stars

for (k = i; k <= rows; ++k) prints rows - i + 1 stars, shrinking as i grows.

Stars
5

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.

Line break
=

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.

2

Variation — User Input Version

Read rows with scanf():

c
#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 > 0 after 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..i to 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 <= i when you meant j < i for spaces (extra space breaks alignment)
  • Using k from 1 to i here—that is Program 3’s growing triangle, not this pattern
  • Forgetting the newline after each row
  • Assuming scanf always succeeds without checking the return value
  • Mixing tabs and spaces in the output

Key Takeaways

1

Row i: print i - 1 spaces, then rows - i + 1 stars.

2

The star loop k = i .. rows counts exactly those stars in one pass.

3

Program 3 grows stars; Program 4 shrinks them—same right column, different profile.

4

Time complexity is O(n²) for n rows.

5

Master Programs 1–4 and you have left, inverted, right-aligned, and inverted right-aligned triangles.

❓ Frequently Asked Questions

Print 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.
That range has length 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.
Program 3 uses spaces 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.
O(n²) for 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.

All Star Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful