Right-Angled Triangle Star Pattern in C

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You'll Learn

How to print a right-angled triangle star pattern in C using nested for loops. This is one of the most fundamental pattern-printing exercises—perfect for beginners learning how the outer loop controls rows and the inner loop controls columns.

The pattern increases by one star per row: row 1 prints *, row 2 prints **, and so on up to the specified number of rows.

⭐ Pattern Output

When you run the program with rows = 5, you’ll see this output:

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

Complete C Program

Here’s the complete C program that prints the right-angled triangle star pattern:

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Declare Variables

int rows = 5; sets the number of rows. Variables i and j are loop counters for rows and columns respectively.

Setup
2

Outer Loop (Rows)

for (i = 1; i <= rows; ++i) iterates from row 1 to row 5. Each iteration of this loop produces one complete line of output.

Row control
3

Inner Loop (Stars)

for (j = 1; j <= i; ++j) runs i times; each time printf("*") appends one star with no newline. Row 1 → 1 star, row 2 → 2 stars, and so on.

Star printing
4

New Line

printf("\n") after the inner loop moves the cursor to the next line, starting a new row of the pattern.

Line break
=

Right-angled triangle

Nested loops yield a right triangle. Total stars = 1+2+…+n = n(n+1)/2O(n²) time, O(1) extra memory. Long rows scroll horizontally inside the green preview on small screens.

2

Variation — User Input Version

Let the user decide the number of rows at runtime using scanf():

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Accept row count from user with scanf()
  • Print an inverted triangle by reversing the outer loop
  • Replace * with numbers for a number triangle
  • Add spaces between stars for visual variety
  • Create a hollow triangle (border stars only)

Avoid

  • Hard-coding the row count in production code
  • Forgetting printf("\n") between rows
  • Using while loops when for is clearer
  • Skipping input validation with scanf()
  • Confusing ++i and i++ in loop context

Key Takeaways

1

The outer loop controls the number of rows, and the inner loop prints stars equal to the current row number.

2

Row i always contains exactly i stars, producing the classic right-triangle shape.

3

Time complexity is O(n²) — total operations = n(n+1)/2.

4

This pattern is the foundation for all other star patterns: pyramid, diamond, hollow shapes, and more.

5

Use scanf() to make the row count dynamic instead of hard-coding it.

❓ Frequently Asked Questions

The outer loop runs from i = 1 to rows. For each row i, the inner loop runs from j = 1 to i, printing one star per iteration. So row 1 gets 1 star, row 2 gets 2 stars, and so on—forming the right-angled triangle.
Nested loops give two-dimensional control: the outer loop handles which row we’re on, and the inner loop handles how many stars to print on that row. This two-level control is required for any 2D pattern.
Yes. Reverse the outer loop: for (i = 5; i >= 1; i--). This starts with 5 stars in the first row and decreases by one each row, producing an upside-down right-angled triangle.
The time complexity is O(n²) where n is the number of rows. The inner loop prints 1+2+3+…+n = n(n+1)/2 total stars, which simplifies to O(n²).

Explore More C Star Patterns!

From pyramids and diamonds to hollow shapes and number patterns—master nested loops with 11+ pattern programs.

All Star Patterns →
Did you know?

The right-angled triangle star pattern is the #1 most-asked pattern in C programming interviews. Mastering it unlocks every other pattern—pyramid, diamond, hollow, and mirrored—because they all build on the same nested-loop foundation.

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