Right-Aligned Right-Angled Triangle Star Pattern in C

What You'll Learn
How to print a right-aligned right-angled triangle in C: each row still has 1, 2, …, rows stars like Program 1, but you print (rows - i) spaces first so the stars shift to the right and the longest row sits flush on the right.
With rows = 5, the first row has four spaces and one star; the last row has no spaces and five stars.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see this output (spaces shown explicitly):
*
**
***
****
*****Complete C Program
Complete C program that prints the right-aligned right-angled triangle:
#include <stdio.h>
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= i; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}🧠 How It Works
Declare Variables
rows is the triangle height. i is the row index; j counts spaces; k counts stars.
Outer Loop (Rows)
for (i = 1; i <= rows; ++i) walks from the top row (one star) to the bottom row (rows stars).
Inner Loop — Spaces
for (j = 1; j <= rows - i; ++j) printf(" "); prints the padding so row i is indented by rows - i columns.
Inner Loop — Stars
for (k = 1; k <= i; ++k) printf("*"); prints exactly i stars after the spaces—same star count as Program 1, different horizontal position.
New Line
printf("\n") ends the row after spaces and stars. Every row has exactly (rows - i) + i = rows characters before the newline, so the pattern stays rows columns wide.
Right-aligned triangle
Each row prints (rows - i) + i = rows characters before the newline — O(rows) per row, O(rows²) overall for rows = n. O(1) extra memory. Full-width lines scroll in the preview on phones.
Variation — User Input Version
Read rows at runtime with scanf():
#include <stdio.h>
int main() {
int rows;
int i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= i; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows > 0afterscanf() - Remove the space loop to get the left-aligned triangle from Program 1
- Use a forward row loop with
(rows - i + 1)stars in one inner loop (same shape, different formulation) - Print digits instead of
*for a right-aligned number triangle - Try a hollow right-aligned triangle (border only)
Avoid
- Swapping
rows - iandibetween the two inner loops - Forgetting
printf("\n")after each row - Using
j < rows - iwhen you meant<=(off-by-one spacing) - Mixing tab characters with spaces (alignment breaks in different terminals)
- Assuming
rowsis valid without checkingscanfreturn value
Key Takeaways
Right alignment = print (rows - i) spaces, then i stars, per row.
Two inner loops are used because spaces and stars have different counts each row.
Star counts per row match Program 1; only the leading padding changes.
Time complexity is O(n²) for n rows (quadratic total output).
This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.
❓ Frequently Asked Questions
i, print (rows - i) spaces, then i stars. The spaces push the stars to the right so the hypotenuse lines up on the right side of the triangle.i grows, while stars increase. One loop can only drive one of those counts cleanly.i per row). Program 3 adds a space loop first so the same i stars appear shifted right.n rows: each row prints Θ(n) characters, and there are n rows.Explore More C Star Patterns!
Pyramids, mirrored shapes, and hollow patterns—all built from spaces, stars, and nested loops.
A right-aligned triangle uses the same star counts as a left-aligned triangle—only leading spaces change. That is why many courses teach Program 1 first, then add the space loop for Program 3.
12 people found this page helpful
