Descending & Ascending Number Pattern in C

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
12345
21234
32123
43212
54321
1

Complete C Program

Two inner loops build each row: one prints descending digits, the other prints ascending digits.

c
#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

1

Outer loop controls rows

i runs from 1 to 5 to print 5 lines.

Rows
2

First inner loop prints descending

for (j = i; j > 1; j--) prints i down to 2.

Desc
3

Second inner loop prints ascending

for (k = 1; k <= 6 - i; k++) prints 1 up to 6-i.

Asc
=

V-shaped sequence per row

Two loops combine into one continuous line of 5 digits per row.

2

Variation — User Input Version

Read the number of rows and build the same two-part row logic dynamically.

c
#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 <= 0 without validating input
  • Forgetting the newline after each row

Key Takeaways

1

Each row is formed by combining a descending part and an ascending part.

2

The descending loop prints from i down to 2.

3

The ascending loop prints from 1 up to rows+1-i.

4

Nested loops are a simple way to build row/column patterns.

❓ Frequently Asked Questions

When i=1, the descending loop doesn’t run (because j > 1 is false), so the row is purely ascending.
In the fixed example, each row prints 5 digits. In the input version, each row prints rows digits.
O(n²) overall for n rows.

Explore More C Number Patterns!

Keep practicing nested loops with more pattern variations.

All Number Patterns →
Did you know?

In the fixed example, the second loop prints exactly 6-i digits, keeping each row length constant at 5.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful