C++ Palindrome Number Triangle Pattern (Outer Peak)

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

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.

Remember
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.

How to Solve It

Outer loop grows i. First inner loop prints i..2; second prints 1..i so the center 1 appears once.

MethodIdeaBest for
Two inner loopsDescend i..2, then ascend 1..iLearning, interviews, exams
Spaced digitsSame loops with cout << j << " "Easier reading for larger peaks

Pseudocode

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

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Left descendingfor (j = i; j > 1; j--) cout << j;
Right ascendingfor (j = 1; j <= i; j++) cout << j;
Avoid double 1Stop the descending loop at j > 1
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach digit on the row
cout << "\n"Ends the current lineAfter both inner loops finish

Print digits without a newline, then end the row once.

Live Preview

Change the row count and the palindrome triangle updates instantly — capped at 9 for readable demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 25 digits
1
212
32123
4321234
543212345

Worked Walkthrough — rows = 5

Trace how descending then ascending halves build each palindrome row around center 1.

iDescendingAscendingPrints
1(none)11
2212212
33212332123
443212344321234
5543212345543212345

Row i prints 2i − 1 digits. Total = 1 + 3 + … + (2n−1) = n² → O(n²).

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.

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

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.

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

Safer input
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.

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

Doubled center

Descending to 1 prints the center twice (321123). Keep j > 1.

swap halves

Broken symmetry

Ascending first then descending gives Program 27’s shape, not this one. Keep desc then asc.

\n inside

Broken rows

If cout << "\n" sits inside either inner loop, the palindrome splits across lines. Call it only after both finish.

rows = 1

Single digit

Output is just 1 — the descending loop never runs. A good sanity check for input validation.

rows > 9

Multi-digit glue

Digits run together (…91011…). Cap demos or add spaces with cout << j << " ".

cin

Check the stream

Validate cin >> rows before looping — a failed read leaves rows unset or stale.

Time and Space Complexity

ProgramTimeExtra 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.

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

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