Descending & Ascending Number Pattern in C

What You’ll Learn
How to print a pattern where each row starts with a short descending sequence, then continues with an ascending sequence starting from 1.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
12345
21234
32123
43212
54321Complete C Program
Two inner loops build each row: one prints descending digits, the other prints ascending digits.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 1; i <= 5; i++) {
for (j = i; j > 1; j--) {
printf("%d", j);
}
for (k = 1; k <= 6 - i; k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer loop controls rows
i runs from 1 to 5 to print 5 lines.
First inner loop prints descending
for (j = i; j > 1; j--) prints i down to 2.
Second inner loop prints ascending
for (k = 1; k <= 6 - i; k++) prints 1 up to 6-i.
V-shaped sequence per row
Two loops combine into one continuous line of 5 digits per row.
Variation — User Input Version
Read the number of rows and build the same two-part row logic dynamically.
#include <stdio.h>
int main() {
int i, j, k;
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = i; j > 1; j--) {
printf("%d", j);
}
for (k = 1; k <= rows + 1 - i; k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces with
printf("%d ", ...)for readability - Swap loop order to make an ascending-then-descending variant
- Use characters instead of numbers for an alphabet version
Avoid
- Using
rows <= 0without validating input - Forgetting the newline after each row
Key Takeaways
Each row is formed by combining a descending part and an ascending part.
The descending loop prints from i down to 2.
The ascending loop prints from 1 up to rows+1-i.
Nested loops are a simple way to build row/column patterns.
❓ Frequently Asked Questions
i=1, the descending loop doesn’t run (because j > 1 is false), so the row is purely ascending.rows digits.n rows.Explore More C Number Patterns!
Keep practicing nested loops with more pattern variations.
In the fixed example, the second loop prints exactly 6-i digits, keeping each row length constant at 5.
12 people found this page helpful
