Palindromic Number Pyramid in C++

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:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1Complete C++ Program
We use one loop for spaces, one for the increasing sequence, and one for the decreasing sequence to form a symmetric pyramid.
#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
Set the number of rows
int rows = 5; controls the height of the pyramid.
Outer loop (rows)
for (i = 1; i <= rows; i++) prints one row per iteration, increasing the row length each time.
Leading spaces (alignment)
for (j = rows; j >= i; j--) prints two spaces per step so the numbers appear centered as a pyramid.
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.
Palindromic pyramid
Each row forms a symmetric sequence like 1 2 3 2 1. For n rows, the work grows roughly like O(n²).
Variation — User Input Version
Read the number of rows with cin to generate a bigger (or smaller) pyramid.
#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
endlrepeatedly in tight loops (slower)
Key Takeaways
Use a space loop to center-align each row of the pyramid.
Print the increasing sequence with 1..i.
Print the decreasing sequence back to 1 to form a palindrome.
This structure applies to many patterns, including pyramids and alphabet pyramids.
❓ Frequently Asked Questions
1..i and then print i-1..1, creating a palindromic sequence like 1 2 3 2 1.cout << k << " "; with cout << k; (and similarly for the decreasing part).rows (or read it from user input). The loops will automatically generate more rows.Explore More C++ Number Patterns!
Practice nested loops with pyramids, diamonds, Floyd’s triangle, and more.
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.
12 people found this page helpful
