Palindromic Number Triangle in C++

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:
1
121
12321
1234321
123454321Complete C++ Program
Print the left half (1..i), then print the right half (i-1..1) to mirror the row.
#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
Set the row count
int rows = 5; defines how many palindromic rows you will print.
Outer loop (row index)
for (i = 1; i <= rows; i++) controls which row is being printed.
First inner loop (ascending 1..i)
for (j = 1; j <= i; j++) prints the left half: 1 2 3 ... i.
Second inner loop (descending i-1..1)
for (k = i - 1; k >= 1; k--) prints the mirrored right half back down to 1.
Palindromic rows
Row i prints 2i-1 digits, so total work is 1+3+5+…+(2n-1)=n² (time complexity O(n²)).
Variation — User Input Version
Let the user decide the number of rows using cin:
#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 usingrows - 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
iagain in the second loop (it would duplicate the center digit) - Hard-coding
5everywhere instead of usingrows - Forgetting the newline after each row
- Using
endlinside loops unnecessarily
Key Takeaways
Each row is built from two parts: ascending then descending.
The second loop starts at i-1 so the middle digit isn’t duplicated.
Total digits printed follow n² because row lengths are 1,3,5,…,(2n-1).
This pattern is a classic stepping stone to diamond and pyramid patterns.
❓ Frequently Asked Questions
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).rows - i leading spaces before the first inner loop to shift each row to the right.i = 1..rows, then print the lower half for i = rows-1..1.Explore More C++ Number Patterns!
Palindromic patterns are a fun way to practice symmetry, loop bounds, and output formatting.
The total number of digits printed in this pattern for n rows is exactly n² because the row lengths are the first n odd numbers: 1, 3, 5, …, (2n-1).
12 people found this page helpful
