⚡ TL;DR
Print the inverted 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 inverted right angle triangle pyramid pattern (also called the reverse half triangle pattern) in the C programming language using stars. Each row contains one fewer star than the previous row, shrinking from n stars down to 1.
Pattern to Print
*****
****
***
**
*Implementation
The C program for the above inverted 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=noRows-1; i>=0; i--) {
for(j=0;j<=i;j++) {
printf("*");
}
printf("\n");
}
return 0;
}Output
Enter number of rows for pattern = 5
*****
****
***
**
*How It Works
This is the mirror image of Star Pattern Series 1 — the only difference is the direction of the outer loop:
- Outer loop (
i) — counts down fromnoRows - 1to0, so the first row printed is the widest. - Inner loop (
j) — still runsi + 1times per row (j <= i). On the first passi = 4, so 5 stars print; on the last passi = 0, so just 1 star prints. - Newline —
printf("\n")ends each row.
Because the inner loop condition j <= i is unchanged, flipping the outer loop’s direction is all it takes to invert the triangle.
Complexity
- Time: O(n²) — total stars printed = n + (n-1) + … + 1 = n(n+1)/2
- Space: O(1) — only loop counters are used
Practice Variations
- Combine this pattern with Series 1 to print a full diamond-like hourglass shape
- Add leading spaces so the inverted triangle is right-aligned
- Replace
*with descending row numbers - Rewrite the same pattern using
whileloops instead offorloops
Happy coding!
