Right-Aligned Descending Number Triangle in C

What You’ll Learn
How to print a right-aligned descending number triangle in C using nested loops. Each row starts at the maximum number (rows) and counts down, but the row length decreases each line.
The key is to keep the outer loop increasing from 1 to rows, while the inner loop counts down from rows to i.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
54321
5432
543
54
5Complete C Program
The inner loop prints from rows down to i, so each next row has one fewer number.
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; ++i) {
for (j = rows; j >= i; --j) {
printf("%d", j);
}
printf("\n");
}
return 0;
}🧠 How It Works
Setup
rows defines the height and the largest printed number.
Outer loop (rows)
i goes from 1 to rows. As i increases, the row length shrinks.
Inner loop (descending print)
j starts at rows and decrements until i, printing rows..i on each row.
New line
printf("\n") moves to the next row.
Right-aligned descending triangle
Total prints are n(n+1)/2 so time complexity is O(n²) for n rows.
Variation — User Input Version
Accept the number of rows from the user using scanf():
#include <stdio.h>
int main() {
int rows;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = rows; j >= i; --j) {
printf("%d", j);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces between numbers with
printf("%d ", j) - Convert to an ascending triangle by flipping the inner loop direction
- Right-align visually with leading spaces (if you add spaces between digits)
- Try different
rowsvalues to see the shape scale
Avoid
- Starting the inner loop at 1 (it won’t start from the maximum)
- Forgetting
printf("\n")after each row - Not validating input when accepting user rows
Key Takeaways
Each row starts at rows and counts down, because the inner loop begins at j = rows.
Row length shrinks because the inner loop stops at j = i.
Total prints are n(n+1)/2, so time complexity is O(n²).
This pattern is closely related to Program 2, but prints numbers in reverse order.
❓ Frequently Asked Questions
j = rows, so the first printed number is always the maximum.j = i. Since i increases each row, fewer values satisfy j >= i, so the row gets shorter.for (j = i; j <= rows; ++j) and print j.Practice More Number Patterns
Changing just the loop start and end values can create dozens of patterns—keep experimenting.
This pattern is a mirror of Program 2’s idea: Program 2 prints 1..rows starting from the row index, while here you print rows..i by counting down.
12 people found this page helpful
