C++ Palindrome Number Pattern (Increasing-Decreasing)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Set m = i. Print the ascending half, step back with m = m - 2, then print the descending half.
Method
Idea
Best for
Two half-loops
Ascend with m++, skip peak, descend with m--
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick 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
Goal
Pattern
Start of row
m = i;
Ascending half
for (j = 1; j <= i; j++) cout << m++;
Skip peak
m = m - 2;
Descending half
for (k = 1; k < i; k++) cout << m--;
Digits on row i
2 * i - 1
End the row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << m++ / cout << m--
Stays on the same line
Each digit
cout << "\n"
Ends the current line
After 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.
Try it
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 result5 rows · 25 digits
1
232
34543
4567654
567898765
Trace
Worked Walkthrough
Trace row i = 3 — ascend to the peak, skip it, then descend.
Step
m
Action
Line so far
Start
3
m = i
Ascend
3 → 4 → 5
print m++ three times
345
Skip peak
6 → 4
m = m - 2
345
Descend
4 → 3
print m-- twice
34543
Without m = m - 2, the peak 5 would print twice. The descending loop runs i - 1 times so the peak stays unique.
Code
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;
}
Output
1
232
34543
4567654
567898765
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;
}
Output (when user enters 4)
Enter the number of rows: 4
1
232
34543
4567654
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;
}
Output
1
232
34543
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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²).