Alternating Ascending & Descending Number Pattern in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Modulo + Loops

What You’ll Learn

How to print a zigzag number pattern where odd rows print numbers in ascending order and even rows print numbers in descending order, while keeping the overall sequence continuous.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
1

Complete C Program

We keep a running start value num. Odd rows print forward; even rows print backward.

c
#include <stdio.h>

int main() {
    int row, col, num = 1;

    for (row = 1; row <= 5; row++) {
        if (row % 2 == 0) {
            for (col = num + row - 1; col >= num; col--) {
                printf("%2d ", col);
            }
        } else {
            for (col = num; col < num + row; col++) {
                printf("%2d ", col);
            }
        }

        printf("\n");
        num += row;
    }

    return 0;
}

🧠 How It Works

1

Track the start value

num is the first number to print on the current row.

State
2

Decide direction using modulo

row % 2 is 0 on even rows, so we print those rows in reverse.

Condition
3

Print row numbers

Odd rows print num .. num+row-1. Even rows print num+row-1 .. num.

Print
4

Advance to the next row

num += row moves the start forward by how many values we printed.

Update
=

Zigzag sequence

Direction alternates per row while the numbers keep increasing overall.

2

Variation — User Input Version

Read the number of rows using scanf().

c
#include <stdio.h>

int main() {
    int row, col, num = 1;
    int rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (row = 1; row <= rows; row++) {
        if (row % 2 == 0) {
            for (col = num + row - 1; col >= num; col--) {
                printf("%2d ", col);
            }
        } else {
            for (col = num; col < num + row; col++) {
                printf("%2d ", col);
            }
        }

        printf("\n");
        num += row;
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Increase spacing with %3d or %4d for larger numbers
  • Swap logic to make odd rows descending and even rows ascending
  • Start from a different value by changing num

Avoid

  • Resetting num inside the loop (breaks continuity)
  • Using rows <= 0 without validation

Key Takeaways

1

Odd rows print ascending; even rows print descending.

2

row % 2 decides which direction to print.

3

num keeps the sequence continuous across rows.

4

Total prints are 1+2+…+n, so runtime is O(n²).

❓ Frequently Asked Questions

To create the zigzag effect. We loop from num+row-1 down to num on even rows.
Initialize num to 100 (and keep the rest the same).
O(n²) for n rows.

Explore More C Number Patterns!

Practice modulo logic and nested loops with more variations.

All Number Patterns →
Did you know?

The total count of printed numbers after n rows is \(1+2+\dots+n = \frac{n(n+1)}{2}\).

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