Right-Angled Triangle Star Pattern in C

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:
*
**
***
****
*****Complete C Program
Here’s the complete C program that prints the right-angled triangle star pattern:
#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
Declare Variables
int rows = 5; sets the number of rows. Variables i and j are loop counters for rows and columns respectively.
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.
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.
New Line
printf("\n") after the inner loop moves the cursor to the next line, starting a new row of the pattern.
Right-angled triangle
Nested loops yield a right triangle. Total stars = 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory. Long rows scroll horizontally inside the green preview on small screens.
Variation — User Input Version
Let the user decide the number of rows at runtime using scanf():
#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
whileloops whenforis clearer - Skipping input validation with
scanf() - Confusing
++iandi++in loop context
Key Takeaways
The outer loop controls the number of rows, and the inner loop prints stars equal to the current row number.
Row i always contains exactly i stars, producing the classic right-triangle shape.
Time complexity is O(n²) — total operations = n(n+1)/2.
This pattern is the foundation for all other star patterns: pyramid, diamond, hollow shapes, and more.
Use scanf() to make the row count dynamic instead of hard-coding it.
❓ Frequently Asked Questions
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.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.Explore More C Star Patterns!
From pyramids and diamonds to hollow shapes and number patterns—master nested loops with 11+ pattern programs.
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.
12 people found this page helpful
