C++ Palindromic Number Pyramid Pattern

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

What Is This Pattern?

A palindromic number pyramid is centered with leading spaces. Row i prints 1..i ascending, then i-1..1 descending — so every line reads the same both ways.

Remember
Rule: spaces (rows-i), then 1..i, then i-1..1

        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1     ← rows = 5

Unlike Program 55 (column-wise 2D fill) or Program 52 (left-aligned, starts at i), this shape always starts at 1 and uses spacing to center the pyramid.

How to Solve It

Outer loop over rows. For each row: print leading spaces, count up to i, count down from i-1, then end the line.

MethodIdeaBest for
Three loopsSpaces + ascending + descendingLearning, interviews, demos
Compact traceSame logic with rows = 3Quick dry-runs on paper

Pseudocode

Pseudocode
for i from 1 to rows:
    for s from 1 to (rows - i):
        print "  "
    for k from 1 to i:
        print k and a space
    for k from (i - 1) down to 1:
        print k and a space
    print newline

Cheat sheet

GoalPattern
Outer row loopfor (i = 1; i <= rows; i++)
Leading spacesfor (s = 1; s <= rows - i; s++) cout << " ";
Ascending halffor (k = 1; k <= i; k++) cout << k << " ";
Descending halffor (k = i - 1; k >= 1; k--) cout << k << " ";
End the rowcout << "\n";
Digits on row i2 * i - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << k << " " / cout << " "Stays on the same lineSpaces and digits
cout << "\n"Ends the lineAfter both number loops

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

Live Preview

Change the row count and the centered palindromic pyramid updates instantly.

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

Live result 5 rows · 25 digits
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Worked Walkthrough

Trace row i = 3 when rows = 5 — spaces, ascending, then descending.

StepLoopPrints
1Spaces: s = 1..2 (2 pairs)
2Ascending: k = 1..31 2 3
3Descending: k = 2..12 1
ResultFull row 31 2 3 2 1

The descending loop starts at i - 1 so the peak digit is printed only once.

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 — spaces, ascending 1..i, then descending i-1..1.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (s = 1; s <= rows - i; s++)
            cout << "  ";

        for (k = 1; k <= i; k++)
            cout << k << " ";

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Center with spaces. Print (rows - i) pairs of " " before any digit on row i.

2. Ascend to the peak. k = 1..i prints the left half including the middle digit.

3. Descend without a double peak. k = i-1..1 mirrors the left half, then "\n" ends the row.

Example 2 — User Input Rows

Read the row count at runtime with a simple validation check.

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

int main()
{
    int rows, i, s, k;

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

    for (i = 1; i <= rows; i++)
    {
        for (s = 1; s <= rows - i; s++)
            cout << "  ";

        for (k = 1; k <= i; k++)
            cout << k << " ";

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Same loop core. Only the source of rows changes from a literal to cin.

2. Entering 4 stops early. You get four centered rows ending at 1 2 3 4 3 2 1.

3. Validate in real apps. Prefer checking cin failure and requiring a positive height (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 spaces and both number loops.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (s = 1; s <= rows - i; s++)
            cout << "  ";

        for (k = 1; k <= i; k++)
            cout << k << " ";

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Only three rows. Easy to dry-run every space pair and digit on paper.

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

3. Peak still once. Row 3 prints 1 2 3 2 1 — ascending includes 3; descending starts at 2.

Edge Cases & Pitfalls

Check these before calling the solution done.

Double peak

Start descending at i - 1

Starting at i prints the middle digit twice (e.g. 1 2 3 3 2 1).

Spacing

Use two spaces per indent step

One space per step looks left-skewed once digits and their trailing spaces are printed.

Alignment

View output in a monospace font

Proportional fonts hide the pyramid shape even when the spaces are correct.

Bad cin

Validate input

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

Time and Space Complexity

ProgramTimeExtra space
Pyramid (Examples 1–3)O(n²)O(1)

Digits printed are 1 + 3 + 5 + … + (2n - 1) = n². Leading spaces add the same order of work. Extra memory is only a few loop variables.

Key Takeaways

  • Rule: spaces (rows - i), then 1..i, then i-1..1.
  • Palindrome: each row reads the same left-to-right and right-to-left.
  • Break the row: call cout << "\n" only after both number loops finish.
  • Complexity: O(n²) time, O(1) extra space.

One line: center with spaces, count up to i, count down from i-1.

Frequently Asked Questions

Each row reads the same forward and backward: 1, 1 2 1, 1 2 3 2 1, and so on.
Print (rows - i) pairs of two spaces before the numbers on row i — more spaces on upper rows, fewer on lower rows.
A centered pyramid where row i shows 1..i ascending then i-1..1 descending. For rows=5, the bottom row is 1 2 3 4 5 4 3 2 1.
Program 55 fills a 2D array column-wise. Program 56 prints palindromic digits directly with spaces, ascending, and descending loops.
Program 52 is left-aligned and starts each row at i (e.g. 34543). Program 56 is centered and always starts at 1 (e.g. 1 2 3 2 1).
Row i already printed the peak in the ascending loop. Starting at i-1 avoids repeating that digit.
Printing a digit (and space) 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 number loops.
O(n²) for n rows because each row prints O(n) spaces and digits, and 1+3+5+...+(2n-1) = n² digits.

Did you know?

Each row prints 1..i..1 with leading spaces for centering. Row i has 2i-1 digits — total prints grow as O(n²) for n rows.

Next: Diagonal Mirror Number Pyramid

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

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