⚡ 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!
