⚡ TL;DR
Print the right angle triangle (half pyramid) star pattern in C using nested for loops. Full source code with output, explanation, and complexity analysis.
In this program, we will see the right angle triangle pyramid pattern (also called the half triangle pattern) in the C programming language using stars. Each row contains one more star than the previous row, growing from 1 star up to n stars.
Pattern to Print
*
**
***
****
*****Implementation
The C program for the above right angle triangle pyramid pattern is as follows:
#include<stdio.h>
int main() {
int i, j, noRows;
printf("Enter number of rows for pattern = ");
scanf("%d",&noRows);
for(i=0; i< noRows; i++) {
for(j=0;j<=i;j++) {
printf("*");
}
printf("\n");
}
return 0;
}Output
Enter number of rows for pattern = 5
*
**
***
****
*****How It Works
Two nested for loops do all the work:
- Outer loop (
i) — runs once per row, from0tonoRows - 1. It decides which row we are printing. - Inner loop (
j) — runsi + 1times on rowi(because ofj <= i), printing exactly one star per iteration. Row 0 prints 1 star, row 1 prints 2, and so on. - Newline — after the inner loop finishes a row,
printf("\n")moves the cursor to the next line.
The key relationship is j <= i: the column count is tied directly to the current row number, which is what produces the growing triangle shape.
Complexity
- Time: O(n²) — total stars printed = 1 + 2 + … + n = n(n+1)/2
- Space: O(1) — only loop counters are used
Practice Variations
- Print the inverted version of this pattern (5 stars down to 1) — see Star Pattern Series 2
- Replace
*with row numbers so each row prints its row number repeatedly - Add leading spaces to convert the half pyramid into a centered full pyramid
- Rewrite the same pattern using
whileloops instead offorloops
Happy coding!
References
- C language reference — cppreference
printfin C — cppreferencescanfin C — cppreference- GCC online documentation — GNU
FAQ
How do I print a right triangle star pattern in C?
Use two nested for loops: the outer loop runs once per row from 0 to n-1, and the inner loop prints i+1 stars using the condition j <= i, so row 0 prints one star and each later row prints one more. A newline printed after the inner loop moves output to the next row.
What is the logic behind the half pyramid pattern in C?
The logic is tying the column count directly to the row number. On row i, the inner loop runs i+1 times, printing one star per iteration, which makes the triangle grow from a single star to n stars. This j <= i relationship is the core idea behind almost every pyramid pattern variation.
What is the time complexity of the triangle star pattern program?
The time complexity is O(n²) because the total number of stars printed is 1 + 2 + … + n, which equals n(n+1)/2 — a quadratic number of print operations. Space complexity is O(1), since the program stores only the two loop counters regardless of how many rows the user requests.
What are some variations of the star triangle pattern in C?
Common variations include the inverted triangle that shrinks from n stars to one, a centered full pyramid made by adding leading spaces, replacing stars with repeated row numbers, and combining two triangles into a diamond shape. You can also rewrite the same output using while loops instead of for loops to practice loop control.

Written by Mihir Pipermitwala
Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.
More about Mihir →