C++ Palindrome Number Pattern (Increasing-Decreasing)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An increasing-decreasing number pyramid prints each row as a palindrome: count up from i to the peak 2i - 1, then back down to i. With rows = 5 you get 1, 232, 34543, 4567654, 567898765.

Remember
Rule: m = i; print up with m++; then m -= 2; print down with m--

1
232
34543
4567654
567898765     ← rows = 5

Unlike Program 51 (one continuous counter across rows), this shape resets m = i on every row and builds a self-contained palindrome.

How to Solve It

Set m = i. Print the ascending half, step back with m = m - 2, then print the descending half.

MethodIdeaBest for
Two half-loopsAscend with m++, skip peak, descend with m--Learning, interviews, demos
Compact traceSame logic with rows = 3Quick dry-runs on paper

Pseudocode

Pseudocode
for i from 1 to rows:
    m = i
    for j from 1 to i:
        print m
        m = m + 1
    m = m - 2
    for k from 1 to (i - 1):
        print m
        m = m - 1
    print newline

Cheat sheet

GoalPattern
Start of rowm = i;
Ascending halffor (j = 1; j <= i; j++) cout << m++;
Skip peakm = m - 2;
Descending halffor (k = 1; k < i; k++) cout << m--;
Digits on row i2 * i - 1
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << m++ / cout << m--Stays on the same lineEach digit
cout << "\n"Ends the current lineAfter both inner loops

Print digits without a newline, then end the row once. Putting "\n" inside either half-loop turns the pyramid into a vertical list.

Live Preview

Change the row count and the palindromic pyramid updates instantly.

Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 25 digits
1
232
34543
4567654
567898765

Worked Walkthrough

Trace row i = 3 — ascend to the peak, skip it, then descend.

StepmActionLine so far
Start3m = i
Ascend3 → 4 → 5print m++ three times345
Skip peak6 → 4m = m - 2345
Descend4 → 3print m-- twice34543

Without m = m - 2, the peak 5 would print twice. The descending loop runs i - 1 times so the peak stays unique.

C++ Programs

Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — ascend with m++, skip the peak, descend with m--.

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

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

    for (i = 1; i <= rows; 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. Seed each row at i. m = i starts the ascending half at the row index.

2. Print up to the peak. The first loop runs i times with m++, reaching 2i - 1.

3. Skip and mirror. m = m - 2 avoids a double peak; the second loop prints i - 1 digits downward, then "\n" ends the row.

Example 2 — User Input Rows

Read the row count at runtime — both halves scale automatically.

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

int main()
{
    int rows, i, j, k, m;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; 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. Same two-loop core. Only the source of rows changes from a literal to cin.

2. Entering 4 stops early. You get four palindromic rows ending at 4567654.

3. Validate in real apps. Prefer checking cin failure and requiring rows >= 1 (tip below).

Safer input tip
if (!(cin >> rows) || rows < 1)
{
    cout << "Please enter a positive integer.\n";
    return 1;
}

Example 3 — Compact rows = 3

Smaller height for quick tracing of m++, m - 2, and m--.

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

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

    for (i = 1; i <= rows; 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. Only three rows. Easy to dry-run every m change on paper.

2. Same formula. Nothing changes except rows — proving the pattern scales.

3. Row 1 has no descent. When i = 1, the decreasing loop never runs — output is just 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

m - 2

Do not skip the peak step-back

Without m = m - 2, the peak digit prints twice and the palindrome breaks.

Descend bound

Use k < i, not k <= i

The descending half must run i - 1 times so the peak stays unique.

Newline

Do not put "\n" inside either half-loop

Call cout << "\n" only after both halves finish.

Bad cin

Validate input

Check cin >> rows and require rows >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)

Row i prints 2i - 1 digits, and 1 + 3 + … + (2n - 1) = n² — quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: set m = i, print up with m++, then m -= 2, then print down with m--.
  • Skip the peak: m = m - 2 prevents a duplicated middle digit.
  • Break the row: call cout << "\n" only after both half-loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i, print i..(2i-1) then back down to i, skipping a second peak, then end the line.

Frequently Asked Questions

Row 3 starts at 3, prints up to 5 (345), then prints back down to 3 (43) after m = m - 2 — producing 34543.
Each row counts up from i to the peak 2i-1, then counts back down to i. The sequence reads the same left-to-right on each line.
After the increasing loop, m is one past the peak. Subtracting 2 moves it to the value just before the peak so the decreasing loop does not repeat the peak digit.
Program 51 uses a continuous counter with alternating direction across rows. Program 52 resets m = i each row and builds a palindromic line per row.
Printing a digit stays on the same line. Printing a newline ends the current row. Digits use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because row i prints 2i-1 digits and 1+3+5+...+(2n-1) = n² total prints.
Step back with m = m - 2 before the decreasing loop. The decreasing loop then runs i-1 times, skipping the peak.
After cin >> rows, check failure and require rows >= 1 before the loops.

Did you know?

Each row is palindromic: print i..(2i-1) ascending, then back down with m = m - 2 to skip the peak. Total digits = 1+3+5+…+(2n-1) = n² — time O(n²).

Next: Mirror Diagonal Pattern

Continue with the next pattern in the C++ number-pattern series.

Program 53 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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