Alternating Row Number Pattern in C++

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

What You’ll Learn

How to print a number triangle where each row contains consecutive numbers, but the direction alternates:

Odd rows print left-to-right (ascending) and even rows print right-to-left (descending).

⭐ Pattern Output

For rows = 5, 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

Keep a running counter k. For odd rows print k upwards; for even rows compute the row end (m) and print it downwards.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j;
    int k = 1, m;

    for (i = 1; i <= 5; i++) {
        m = k + i - 1;

        for (j = 1; j <= i; j++) {
            if (i % 2 == 1)
                cout << k << " ";
            else
                cout << m-- << " ";

            k++;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Track the next number

k starts at 1 and always moves forward, so numbers across rows remain consecutive.

Counter
2

Decide the row direction

Use i % 2: odd rows print ascending, even rows print descending.

Odd/Even
3

Compute the last number of the row

m = k + i - 1 is the last value that belongs to the current row (because the row prints i numbers).

Row end
4

Print and advance

Odd row: print k. Even row: print m--. Either way, increment k so the next row continues the sequence.

Printing
=

Alternating rows

The numbers keep increasing overall, but each row flips direction based on parity.

2

Variation — User Input Version

Let the user choose the number of rows at runtime using cin:

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    if (rows <= 0) return 0;

    int k = 1;
    for (int i = 1; i <= rows; i++) {
        int m = k + i - 1;

        for (int j = 1; j <= i; j++) {
            if (i % 2 == 1)
                cout << k << " ";
            else
                cout << m-- << " ";

            k++;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print without trailing spaces by building a row string first
  • Increase rows to see larger alternating blocks
  • Right-align the triangle by printing leading spaces per row
  • Swap the rule to start with descending on row 1
  • Replace numbers with letters for an alphabet variant

Avoid

  • Forgetting to update k on every printed element
  • Using endl inside loops (extra flushing)
  • Printing m but never decrementing it on even rows
  • Accepting non-positive rows without input validation

Key Takeaways

1

Odd rows print the row’s numbers in ascending order.

2

Even rows print the same row range in descending order using m--.

3

k advances on every print, keeping the overall sequence consecutive.

4

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

❓ Frequently Asked Questions

Because for even i, the program prints from m = k + i - 1 down to the start of the row using m--.
k increments for every printed element, even when the row prints in reverse using m.
Replace the fixed 5 with a variable rows and loop i from 1 to rows.
O(n²) for n rows because the total prints are 1+2+…+n.

Explore More C++ Number Patterns!

Alternating direction by row is a great way to practice parity checks and careful counter updates.

All Number Patterns →
Did you know?

Instead of printing directly, you can build each row into an array and reverse it on even rows. It’s often easier when you want perfect spacing or formatting.

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