Increasing-Decreasing Number Pattern in C++

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

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:

Output
1
232
34543
4567654
567898765
1

Complete C++ Program

For each row i, print i increasing numbers, then print i-1 decreasing numbers to mirror the row.

C++
#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

1

Pick the row number

The outer loop runs i = 1..rows. Each value of i forms one row.

Rows
2

Initialize the starting value

Set m = i so the row begins at the row number.

Start
3

Print the increasing part

The first inner loop prints i numbers: m, m+1, ... by printing m++.

Increase
4

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.

Decrease
=

A mirrored row pattern

Row length is 2i-1, and total work is about O(n²) for n rows.

2

Variation — User Input Version

Let the user decide 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;

    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-1 row length growth

Avoid

  • Starting the decreasing loop at the peak (it will duplicate the middle number)
  • Using endl inside loops (extra flushing)
  • Mixing up loop bounds (first loop runs i times, second runs i-1 times)
  • Ignoring invalid input (handle non-positive rows)

Key Takeaways

1

Each row starts from the row number i.

2

The first inner loop prints the increasing sequence.

3

The second inner loop prints the decreasing sequence without repeating the peak.

4

Total work is about O(n²) for n rows.

❓ Frequently Asked Questions

Because the row prints an increasing sequence and then mirrors it back down (excluding the peak), creating symmetry.
After the increasing loop, m is one past the last printed value. Subtracting 2 moves it to the second-last value so the peak isn’t printed twice.
Print a space after each number, for example cout << m++ << ' '; and cout << m-- << ' ';.
O(n²) for n rows because total prints are 1+2+…+n.

Explore More C++ Number Patterns!

Mirrored patterns are excellent practice for understanding loop bounds and avoiding duplicate midpoints.

All Number Patterns →
Did you know?

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.

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