Right-Aligned Right-Angled Triangle Star Pattern in C

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

What You'll Learn

How to print a right-aligned right-angled triangle in C: each row still has 1, 2, …, rows stars like Program 1, but you print (rows - i) spaces first so the stars shift to the right and the longest row sits flush on the right.

With rows = 5, the first row has four spaces and one star; the last row has no spaces and five stars.

⭐ Pattern Output

When you run the program with rows = 5, you’ll see this output (spaces shown explicitly):

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

Complete C Program

Complete C program that prints the right-aligned right-angled triangle:

c
#include <stdio.h>

int main() {
    int rows = 5;
    int i, j, k;

    for (i = 1; i <= rows; ++i) {
        for (j = 1; j <= rows - i; ++j) {
            printf(" ");
        }
        for (k = 1; k <= i; ++k) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Declare Variables

rows is the triangle height. i is the row index; j counts spaces; k counts stars.

Setup
2

Outer Loop (Rows)

for (i = 1; i <= rows; ++i) walks from the top row (one star) to the bottom row (rows stars).

Row control
3

Inner Loop — Spaces

for (j = 1; j <= rows - i; ++j) printf(" "); prints the padding so row i is indented by rows - i columns.

Right align
4

Inner Loop — Stars

for (k = 1; k <= i; ++k) printf("*"); prints exactly i stars after the spaces—same star count as Program 1, different horizontal position.

Stars
5

New Line

printf("\n") ends the row after spaces and stars. Every row has exactly (rows - i) + i = rows characters before the newline, so the pattern stays rows columns wide.

Line break
=

Right-aligned triangle

Each row prints (rows - i) + i = rows characters before the newline — O(rows) per row, O(rows²) overall for rows = n. O(1) extra memory. Full-width lines scroll in the preview on phones.

2

Variation — User Input Version

Read rows at runtime 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 <= rows - i; ++j) {
            printf(" ");
        }
        for (k = 1; k <= i; ++k) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate rows > 0 after scanf()
  • Remove the space loop to get the left-aligned triangle from Program 1
  • Use a forward row loop with (rows - i + 1) stars in one inner loop (same shape, different formulation)
  • Print digits instead of * for a right-aligned number triangle
  • Try a hollow right-aligned triangle (border only)

Avoid

  • Swapping rows - i and i between the two inner loops
  • Forgetting printf("\n") after each row
  • Using j < rows - i when you meant <= (off-by-one spacing)
  • Mixing tab characters with spaces (alignment breaks in different terminals)
  • Assuming rows is valid without checking scanf return value

Key Takeaways

1

Right alignment = print (rows - i) spaces, then i stars, per row.

2

Two inner loops are used because spaces and stars have different counts each row.

3

Star counts per row match Program 1; only the leading padding changes.

4

Time complexity is O(n²) for n rows (quadratic total output).

5

This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.

❓ Frequently Asked Questions

For row i, print (rows - i) spaces, then i stars. The spaces push the stars to the right so the hypotenuse lines up on the right side of the triangle.
You need separate loops (or equivalent logic) for two different repeat counts: spaces decrease as i grows, while stars increase. One loop can only drive one of those counts cleanly.
Program 1 prints only stars (i per row). Program 3 adds a space loop first so the same i stars appear shifted right.
O(n²) for n rows: each row prints Θ(n) characters, and there are n rows.

Explore More C Star Patterns!

Pyramids, mirrored shapes, and hollow patterns—all built from spaces, stars, and nested loops.

All Star Patterns →
Did you know?

A right-aligned triangle uses the same star counts as a left-aligned triangle—only leading spaces change. That is why many courses teach Program 1 first, then add the space loop for Program 3.

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