C++ Palindrome Number Triangle Pattern (Outer Peak)

Definition
What Is This Pattern?
A palindrome number triangle prints descending digits i..2, then ascending 1..i — each row reads the same forwards and backwards, with center digit 1.
Rule: for i from 1 to rows
print j from i down to 2
print j from 1 up to i
1
212
32123
4321234
543212345 ← rows = 5
Follows the right-aligned decreasing triangle in Program 36; next is the decreasing-length sequential triangle in Program 38.
Approach
How to Solve It
Outer loop grows i. First inner loop prints i..2; second prints 1..i so the center 1 appears once.
| Method | Idea | Best for |
|---|
| Two inner loops | Descend i..2, then ascend 1..i | Learning, interviews, exams |
| Spaced digits | Same loops with cout << j << " " | Easier reading for larger peaks |
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j
for j from 1 to i:
print j
print newline
Cheat sheet
| Goal | Pattern |
|---|
| Grow each row | for (i = 1; i <= rows; i++) |
| Left descending | for (j = i; j > 1; j--) cout << j; |
| Right ascending | for (j = 1; j <= i; j++) cout << j; |
| Avoid double 1 | Stop the descending loop at j > 1 |
| End of row | cout << "\n"; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
cout << j | Stays on the same line | Each digit on the row |
cout << "\n" | Ends the current line | After both inner loops finish |
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly — capped at 9 for readable demos.
Trace
Worked Walkthrough — rows = 5
Trace how descending then ascending halves build each palindrome row around center 1.
i | Descending | Ascending | Prints |
|---|
1 | (none) | 1 | 1 |
2 | 2 | 12 | 212 |
3 | 32 | 123 | 32123 |
4 | 432 | 1234 | 4321234 |
5 | 5432 | 12345 | 543212345 |
Row i prints 2i − 1 digits. Total = 1 + 3 + … + (2n−1) = n² → O(n²).
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 — descend i..2, then ascend 1..i.
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
cout << j;
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
1
212
32123
4321234
543212345
How It Works
1. Outer grows the peak. i is both the row index and the outer digit on each half.
2. Descend then ascend. Print i..2, then 1..i so the center 1 is not repeated.
3. Newline once. Call cout << "\n" only after both inner loops finish.
Example 2 — User Input Rows
Read rows with cin, validate, then use the same two-loop core.
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
cout << j;
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Descend + ascend matches Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
if (!(cin >> rows) || rows < 1 || rows > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm the descending loop stops at 2.
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
cout << j;
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
How It Works
1. Three rows. i = 1 → 1; i = 2 → 212; i = 3 → 32123.
2. Trace on paper. If the descending loop goes to 1, you get a doubled center (321123).
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 1Doubled center
Descending to 1 prints the center twice (321123). Keep j > 1.
swap halvesBroken symmetry
Ascending first then descending gives Program 27’s shape, not this one. Keep desc then asc.
\n insideBroken rows
If cout << "\n" sits inside either inner loop, the palindrome splits across lines. Call it only after both finish.
rows = 1Single digit
Output is just 1 — the descending loop never runs. A good sanity check for input validation.
rows > 9Multi-digit glue
Digits run together (…91011…). Cap demos or add spaces with cout << j << " ".
cinCheck the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Fixed / input (Examples 1–2) | O(n²) | O(1) |
| Compact rows = 3 (Example 3) | O(n²) | O(1) |
Row i prints 2i − 1 digits. Total = 1 + 3 + … + (2n−1) = n² → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Two halves: print i..2, then 1..i — center is always 1.
Stop at 2: the descending loop must not print the center 1.
Break the row: digits in both inners; cout << "\n" after both finish.
Complexity: O(n²) time from n² digits; O(1) extra space.
One line: for each row i, print i..2 then 1..i, then newline.
Frequently Asked Questions
A palindrome number triangle: for rows=5 you get 1 / 212 / 32123 / 4321234 / 543212345 — each row reads the same forwards and backwards.
Each row reads the same forward and backward. For example, 4321234 is symmetric around the center digit 1.
First, a loop prints i down to 2 (left half). Then another loop prints 1 up to i (right half). Together they create a mirrored sequence.
The first loop prints 4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.
Program 27 prints 1..i then i-1..1 (peak in the middle). Program 37 prints i..2 then 1..i (center digit is always 1).
cout << j prints digits on the same line. cout << "\n" ends the row after both inner loops finish.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digits are 1 + 3 + 5 + … + (2n-1) = n².
🤔
Did you know?
Each row is a palindrome: print i down to 2, then 1 up to i. Row i prints 2i - 1 digits — total digits across all rows = n².
Next: Decreasing-Length Sequential Triangle
Continue with a triangle whose rows get shorter while values keep counting.
Program 38 tutorial →About the author
Developer, cloud engineer, and technical writer
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