Ascending Number Pattern with Right Padding in C

What You’ll Learn
How to print an ascending number pattern with right padding in C. Each row begins with an ascending sequence, then fills the rest of the row with the maximum value (the row count).
This is a great exercise for splitting row output into two parts: sequence + padding.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5Complete C Program
The first inner loop prints i..rows. The second inner loop prints rows repeatedly to fill the right side.
#include <stdio.h>
int main() {
int rows = 5;
int i, j, k;
for (i = rows; i >= 1; --i) {
for (j = i; j <= rows; ++j) {
printf("%d ", j);
}
for (k = 1; k < i; ++k) {
printf("%d ", rows);
}
printf("\n");
}
return 0;
}🧠 How It Works
Set the maximum value
rows = 5 defines both the triangle size and the right padding number.
Outer loop counts down
for (i = rows; i >= 1; --i) decides the first number of each row.
Print the ascending sequence
for (j = i; j <= rows; ++j) prints i i+1 ... rows.
Pad the right side with rows
for (k = 1; k < i; ++k) prints rows repeatedly so each row has exactly rows numbers.
Sequence + padding per row
Each row prints exactly rows numbers. Total operations \(\approx rows \times rows\), so time complexity is O(n²).
Variation — User Input Version
Let the user choose the number of rows at runtime:
#include <stdio.h>
int main() {
int rows;
int i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = i; j <= rows; ++j) {
printf("%d ", j);
}
for (k = 1; k < i; ++k) {
printf("%d ", rows);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Remove the padding loop to print only the ascending sequence per row
- Change the padding value (use
ior0) to create new patterns - Right-align by printing leading spaces before the sequence
- Print without trailing spaces by formatting the last number differently
- Replace numbers with characters to create padded alphabet patterns
Avoid
- Mixing up loop bounds (keep the sequence as
j = i..rows) - Forgetting the newline after each row
- Using uninitialized variables when moving code around
- Assuming fixed rows when you want a scalable program
- Letting invalid input (like negative rows) pass unchecked
Key Takeaways
Each row prints an ascending sequence from i up to rows.
The remaining positions are filled using right padding (printing rows).
Two inner loops keep the row logic clear: one for the sequence, one for the padding.
Total work is proportional to n² for n rows.
❓ Frequently Asked Questions
i = rows, so the ascending sequence is just 5. The rest of the row is filled by the padding loop, which also prints 5.rows numbers, giving the right-filled block effect.i..rows, such as 5, 4 5, 3 4 5 and so on.Explore More C Number Patterns!
Learn spacing, alignment, and padding techniques by practicing more number patterns.
Many grid-like patterns are just two parts per row: print what changes first, then fill the rest with a constant (padding). This page is a simple example of that idea.
12 people found this page helpful
