Right-Aligned Descending Number Triangle in C

What You’ll Learn
How to print a right-aligned descending number triangle in C. Each row prints values from the row number down to 1.
Leading spaces are printed before each row so the triangle aligns on the right.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
21
321
4321
54321Complete C Program
First print rows - i spaces, then print i..1 in descending order.
#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 = i; k >= 1; k--) {
printf("%d", k);
}
printf("\n");
}
return 0;
}🧠 How It Works
Choose the number of rows
int rows = 5; sets how many lines to print.
Print leading spaces
rows - i spaces push numbers to the right so each row aligns to the right edge.
Print descending numbers
for (k = i; k >= 1; k--) prints i..1 each row.
Right-aligned triangle
Spaces decrease as rows increase, producing a right-aligned triangular shape.
Variation — User Input Version
Read rows from the user and print the same right-aligned pattern.
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int i, j, k;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (k = i; k >= 1; k--) {
printf("%d", k);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Print ascending numbers instead of descending for a new look
- Remove spaces to make it left-aligned
- Add spaces between numbers with
printf("%d ", k) - Use characters instead of numbers to create alphabet patterns
- Convert it into a centered pyramid by adjusting spaces
Avoid
- Forgetting the newline after each row
- Using tabs for spacing (alignment varies)
- Not validating user input for negative rows
- Mixing up which loop prints spaces vs numbers
Key Takeaways
The space loop controls right alignment (rows - i).
The number loop prints i..1 to create descending rows.
Right alignment is just a spacing trick added before the numbers.
Nested loops are the foundation for most pattern problems.
❓ Frequently Asked Questions
i..1) and starts with rows - i spaces, so all rows align to the right.Explore More C Number Patterns!
Right alignment and spacing logic show up in many advanced pattern problems.
Right-aligned patterns are usually just left-aligned patterns plus a simple space loop. Once you master spacing, you can create centered and diamond patterns too.
12 people found this page helpful
