Increasing-Decreasing Number Pattern in C++

What You’ll Learn
How to print a pattern where each row starts at the row number, counts up, then mirrors back down:
1, 232, 34543, 4567654, 567898765.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
232
34543
4567654
567898765Complete C++ Program
For each row i, print i increasing numbers, then print i-1 decreasing numbers to mirror the row.
#include <iostream>
using namespace std;
int main() {
int i, j, k, m;
for (i = 1; i <= 5; i++) {
m = i;
for (j = 1; j <= i; j++)
cout << m++;
m = m - 2;
for (k = 1; k < i; k++)
cout << m--;
cout << "\n";
}
return 0;
}🧠 How It Works
Pick the row number
The outer loop runs i = 1..rows. Each value of i forms one row.
Initialize the starting value
Set m = i so the row begins at the row number.
Print the increasing part
The first inner loop prints i numbers: m, m+1, ... by printing m++.
Mirror back down
After the increase, m is one step past the end. Set m = m - 2 and print i-1 values using m-- to avoid repeating the peak twice.
A mirrored row pattern
Row length is 2i-1, and total work is about O(n²) for n rows.
Variation — User Input Version
Let the user decide 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;
for (int i = 1; i <= rows; i++) {
int m = i;
for (int j = 1; j <= i; j++)
cout << m++;
m = m - 2;
for (int k = 1; k < i; k++)
cout << m--;
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces between digits for readability (e.g., print
<< ' ') - Center-align the pyramid by printing leading spaces before each row
- Replace numbers with characters to create alphabet palindromes
- Use a string builder per row if you want perfect spacing
- Increase rows to observe the
2i-1row length growth
Avoid
- Starting the decreasing loop at the peak (it will duplicate the middle number)
- Using
endlinside loops (extra flushing) - Mixing up loop bounds (first loop runs
itimes, second runsi-1times) - Ignoring invalid input (handle non-positive rows)
Key Takeaways
Each row starts from the row number i.
The first inner loop prints the increasing sequence.
The second inner loop prints the decreasing sequence without repeating the peak.
Total work is about O(n²) for n rows.
❓ Frequently Asked Questions
m is one past the last printed value. Subtracting 2 moves it to the second-last value so the peak isn’t printed twice.cout << m++ << ' '; and cout << m-- << ' ';.Explore More C++ Number Patterns!
Mirrored patterns are excellent practice for understanding loop bounds and avoiding duplicate midpoints.
Many palindromic patterns use the same trick: print an increasing sequence, then print a decreasing sequence starting from the second-last element to avoid duplicating the center.
12 people found this page helpful
