Right-Aligned Reverse Number Triangle in C

What You’ll Learn
How to print a right-aligned reverse number triangle in C. Each row prints numbers from a maximum value (like 5) down to a row-specific minimum, and leading spaces align the triangle to the right.
This is a good practice problem for understanding nested loops, spacing logic, and descending sequences.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1Complete C Program
We print rows - i groups of spaces, then print values from rows down to rows - i + 1.
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = rows; k >= (rows - i + 1); k--) {
printf("%d ", k);
}
printf("\n");
}
return 0;
}🧠 How It Works
Pick the maximum value
With rows = 5, the maximum printed value is also 5.
Outer loop decides the row length
Row i prints exactly i numbers.
Spaces create right alignment
The loop printing " " runs rows - i times, pushing numbers to the right.
Descending sequence per row
We print k from rows down to rows - i + 1, which yields 5; then 5 4; then 5 4 3; and so on.
Right-aligned reverse triangle
Total printed values are 1+2+…+n = n(n+1)/2, so runtime is O(n²) for n rows.
Variation — User Input Version
Accept the row count using scanf():
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = rows; k >= (rows - i + 1); k--) {
printf("%d ", k);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Remove the space loop to make it left-aligned
- Print ascending values by looping from
rows - i + 1up torows - Use a different maximum value by changing
rows - Turn it into a centered pyramid by adjusting spaces
- Swap numbers for letters to create alphabet patterns
Avoid
- Forgetting
printf("\n")after each row - Printing the wrong number of spaces (breaks alignment)
- Using an incorrect stop condition for the descending loop
- Assuming rows can be negative (validate user input)
Key Takeaways
Leading spaces make the triangle right-aligned.
Each row prints a descending sequence from rows.
The minimum per row is rows - i + 1.
Runtime is O(n²) for n rows.
❓ Frequently Asked Questions
k = rows on every row, so the first printed value is always the maximum.printf(" ") before the numbers.Explore More C Number Patterns!
Right-aligned patterns are great practice for spacing logic—keep going with the next program.
Many right-aligned patterns rely on the same idea: print spaces first, then print the row content. Once you master spacing, pyramids and diamonds become much easier.
12 people found this page helpful
