Alternating Row Number Pattern in C++

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:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15Complete 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.
#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
Track the next number
k starts at 1 and always moves forward, so numbers across rows remain consecutive.
Decide the row direction
Use i % 2: odd rows print ascending, even rows print descending.
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).
Print and advance
Odd row: print k. Even row: print m--. Either way, increment k so the next row continues the sequence.
Alternating rows
The numbers keep increasing overall, but each row flips direction based on parity.
Variation — User Input Version
Let the user choose the number of rows at runtime using cin:
#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
rowsto 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
kon every printed element - Using
endlinside loops (extra flushing) - Printing
mbut never decrementing it on even rows - Accepting non-positive rows without input validation
Key Takeaways
Odd rows print the row’s numbers in ascending order.
Even rows print the same row range in descending order using m--.
k advances on every print, keeping the overall sequence consecutive.
Total prints are \(1+2+\dots+n\), so runtime is about O(n²).
❓ Frequently Asked Questions
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.5 with a variable rows and loop i from 1 to rows.Explore More C++ Number Patterns!
Alternating direction by row is a great way to practice parity checks and careful counter updates.
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.
12 people found this page helpful
