Palindromic Number Pyramid in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a palindromic number pyramid in C++ using nested loops. Each row prints numbers from 1 up to the row value, then back down to 1.

You’ll also learn how to use a separate loop to print leading spaces so the pyramid stays aligned.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
      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
1

Complete C++ Program

We use one loop for spaces, one for the increasing sequence, and one for the decreasing sequence to form a symmetric pyramid.

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

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

    for (i = 1; i <= rows; i++) {
        for (j = rows; j >= i; j--) {
            cout << "  ";
        }

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

        n = k - 1;
        for (m = 1; m < i; m++) {
            cout << --n << " ";
        }

        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Set the number of rows

int rows = 5; controls the height of the pyramid.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) prints one row per iteration, increasing the row length each time.

Row control
3

Leading spaces (alignment)

for (j = rows; j >= i; j--) prints two spaces per step so the numbers appear centered as a pyramid.

Spacing
4

Print 1..i then i-1..1

First, for (k = 1; k <= i; k++) prints 1..i. Then we set n = k - 1 and print the decreasing part using cout << --n.

Numbers
=

Palindromic pyramid

Each row forms a symmetric sequence like 1 2 3 2 1. For n rows, the work grows roughly like O(n²).

2

Variation — User Input Version

Read the number of rows with cin to generate a bigger (or smaller) pyramid.

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

int main() {
    int i, j, k, m, n;
    int rows;

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

    for (i = 1; i <= rows; i++) {
        for (j = rows; j >= i; j--) {
            cout << "  ";
        }

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

        n = k - 1;
        for (m = 1; m < i; m++) {
            cout << --n << " ";
        }

        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add input validation (e.g., ensure rows > 0)
  • Print tabs/spaces differently to adjust pyramid width
  • Remove spaces between numbers to form a compact pyramid
  • Print a centered pyramid of characters instead of numbers
  • Use functions to separate spacing and number printing logic

Avoid

  • Forgetting to print a newline after each row
  • Hardcoding row count everywhere (use rows)
  • Mixing alignment and number logic in one loop
  • Using endl repeatedly in tight loops (slower)

Key Takeaways

1

Use a space loop to center-align each row of the pyramid.

2

Print the increasing sequence with 1..i.

3

Print the decreasing sequence back to 1 to form a palindrome.

4

This structure applies to many patterns, including pyramids and alphabet pyramids.

❓ Frequently Asked Questions

Because we print 1..i and then print i-1..1, creating a palindromic sequence like 1 2 3 2 1.
Replace cout << k << " "; with cout << k; (and similarly for the decreasing part).
Increase rows (or read it from user input). The loops will automatically generate more rows.
O(n²) for n rows, because each row prints up to \(2i-1\) numbers (plus spaces), and there are n rows.

Explore More C++ Number Patterns!

Practice nested loops with pyramids, diamonds, Floyd’s triangle, and more.

All Number Patterns →
Did you know?

Palindromic pyramids are a great way to practice loop control and alignment. Once you’re comfortable with this, you can generate diamonds by printing the pyramid up and then down.

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