C++ Palindrome Number Triangle Pattern

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

What Is This Pattern?

A palindrome number triangle prints ascending digits 1..i, then mirrors with i-1..1 — each row reads the same forwards and backwards.

Remember
Rule: for i from 1 to rows
        print j from 1 to i
        print k from i-1 down to 1

1
121
12321
1234321
123454321     ← rows = 5

Follows the diagonal asterisk pattern in Program 26; next is the 0-centered descending mirror in Program 28.

How to Solve It

Outer loop grows i. First inner loop prints 1..i; second prints i-1..1 so the peak digit appears once.

MethodIdeaBest for
Two inner loopsAscend 1..i, then descend i-1..1Learning, interviews, exams
Spaced digitsSame loops with cout << j << " "Easier reading for larger peaks

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j
    for k from i - 1 down to 1:
        print k
    print newline

Cheat sheet

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Left ascendingfor (j = 1; j <= i; j++) cout << j;
Right mirrorfor (k = i - 1; k >= 1; k--) cout << k;
Avoid double peakStart the mirror at i - 1, not i
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / kStays 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
121
12321
1234321
123454321

Worked Walkthrough — rows = 5

Trace how ascending then descending halves build each palindrome row.

iAscendingMirrorPrints
11(none)1
2121121
31232112321
412343211234321
5123454321123454321

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 — ascend 1..i, then mirror i-1..1.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= i; j++)
            cout << j;

        for (k = i - 1; k >= 1; k--)
            cout << k;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer grows the peak. i is both the row index and the middle digit.

2. Ascend then mirror. Print 1..i, then i-1..1 so the peak 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, k;

    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 = 1; j <= i; j++)
            cout << j;

        for (k = i - 1; k >= 1; k--)
            cout << k;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Ascend + mirror 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 mirror start at i - 1.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= i; j++)
            cout << j;

        for (k = i - 1; k >= 1; k--)
            cout << k;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 1 → 1; i = 2 → 121; i = 3 → 12321.

2. Trace on paper. If the mirror starts at i instead of i - 1, you get a doubled peak (123321).

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.

k = i

Doubled peak

Starting the mirror at i prints the middle digit twice (123321). Use k = i - 1.

no mirror

Missing descending loop

Without the second loop you get 1, 12, 123 — not palindromes. Keep both inners.

\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 mirror 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 1..i, then mirror with i-1..1.
  • Start at i-1: the descending loop must not repeat the peak digit.
  • 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 1..i then i-1..1, then newline.

Frequently Asked Questions

A palindrome number triangle: for rows=5 you get 1 / 121 / 12321 / 1234321 / 123454321 — each row reads the same forwards and backwards.
Starting from i-1 avoids repeating the middle number. For i=3, printing 123 then 21 makes 12321.
The ascending loop prints 1..i and the descending loop prints i-1..1 — together they read the same forwards and backwards.
cout << j prints digits on the same line. cout << "\n" ends the row after both inner loops finish.
One loop handles the ascending half and the other handles the mirror half — clearer than a single complex loop.
Use cout << j << " " and cout << k << " " in the loops instead of cout << j.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because row i prints 2i-1 digits and the total is 1+3+5+…+(2n-1) = n².

Did you know?

This palindrome triangle prints 1..i and then i-1..1 on each row. The second loop mirrors the first, producing outputs like 12321 and 123454321.

Next: 0-Centered Descending Mirror

Continue with a mirror pattern that places 0 in the center.

Program 28 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