Palindromic Number Triangle in C++

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

What You’ll Learn

How to print a palindromic number triangle in C++ where each row increases from 1 to the row number and then decreases back to 1.

This pattern is a great exercise for building symmetry using two inner loops: one ascending and one descending.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
121
12321
1234321
123454321
1

Complete C++ Program

Print the left half (1..i), then print the right half (i-1..1) to mirror the row.

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

Set the row count

int rows = 5; defines how many palindromic rows you will print.

Setup
2

Outer loop (row index)

for (i = 1; i <= rows; i++) controls which row is being printed.

Row control
3

First inner loop (ascending 1..i)

for (j = 1; j <= i; j++) prints the left half: 1 2 3 ... i.

Left half
4

Second inner loop (descending i-1..1)

for (k = i - 1; k >= 1; k--) prints the mirrored right half back down to 1.

Right half
=

Palindromic rows

Row i prints 2i-1 digits, so total work is 1+3+5+…+(2n-1)=n² (time complexity O(n²)).

2

Variation — User Input Version

Let the user decide the number of rows using cin:

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

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

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

    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;
}

💡 Tips for Enhancement

Try These

  • Validate input (e.g., check cin.fail()) before using rows
  • Add spaces between digits with cout << j << ' ' to improve readability
  • Right-align the triangle by printing leading spaces before each row
  • Print the same idea with characters (A, ABA, ABCBA, ...)
  • Use a different starting number by printing base + j - 1

Avoid

  • Printing i again in the second loop (it would duplicate the center digit)
  • Hard-coding 5 everywhere instead of using rows
  • Forgetting the newline after each row
  • Using endl inside loops unnecessarily

Key Takeaways

1

Each row is built from two parts: ascending then descending.

2

The second loop starts at i-1 so the middle digit isn’t duplicated.

3

Total digits printed follow because row lengths are 1,3,5,…,(2n-1).

4

This pattern is a classic stepping stone to diamond and pyramid patterns.

❓ Frequently Asked Questions

Because i is already printed at the end of the first loop. Starting from i-1 avoids duplicating the center digit (otherwise you’d get 1233321 for i=3).
Yes. Print rows - i leading spaces before the first inner loop to shift each row to the right.
Print this triangle for i = 1..rows, then print the lower half for i = rows-1..1.
O(n²) because the total digits printed are 1+3+5+…+(2n-1) = n².

Explore More C++ Number Patterns!

Palindromic patterns are a fun way to practice symmetry, loop bounds, and output formatting.

All Number Patterns →
Did you know?

The total number of digits printed in this pattern for n rows is exactly because the row lengths are the first n odd numbers: 1, 3, 5, …, (2n-1).

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