The inverted palindrome number pyramid starts with 123454321 and shrinks to a single 1. Every row reads the same forwards and backwards, and each one is two digits shorter than the row above.
Quick answer: Row i has a peak of k = n β i + 1. Print i β 1 spaces, the numbers 1 to k, then k β 1 down to 1. The peak drops by one every row, so the rows shrink by two digits and the pyramid points down.
This program is part of the Pyramid Patterns collection.
Table of contents8 sections
- Inverted Palindrome Number Pyramid output
- How the inverted palindrome number pyramid works
- Inverted Palindrome Number Pyramid in C
- Inverted Palindrome Number Pyramid in C++
- Inverted Palindrome Number Pyramid in Java
- Inverted Palindrome Number Pyramid in Python
- Time and space complexity
- Frequently asked questions
Inverted Palindrome Number Pyramid output
Output (n = 5):
123454321
1234321
12321
121
1
Same shape, other characters: the Inverted Pyramid Star Pattern program draws this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the inverted palindrome number pyramid works
This is the palindrome number pyramid printed from its widest row upwards. Only the peak changes from row to row: 5, 4, 3, 2, 1. Each row is built the same way, climb to the peak and come back down without repeating it. One extra leading space per row keeps the centre in place, because the row loses one digit on each side.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
i β 1spaces. - Print
n β i + 1numbers (valuej). - Print
n β inumbers (valuen β i + 1 β j). - Move to the next line.
Key takeaways
- The peak
k = n β i + 1falls by one each row. - Every row is a palindrome that skips the repeated peak.
- One more leading space per row keeps the rows centred.
- Put it under the upright palindrome pyramid, from row 2, to get the palindrome number diamond.
Inverted Palindrome Number Pyramid in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++)
printf(" ");
for (int j = 1; j <= n - i + 1; j++)
printf("%d", j);
for (int j = 1; j <= n - i; j++)
printf("%d", n - i + 1 - j);
printf("\n");
}
return 0;
}Inverted Palindrome Number Pyramid in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++)
cout << " ";
for (int j = 1; j <= n - i + 1; j++)
cout << j;
for (int j = 1; j <= n - i; j++)
cout << n - i + 1 - j;
cout << endl;
}
return 0;
}Inverted Palindrome Number Pyramid in Java
public class InvertedPalindromeNumberPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++)
System.out.print(" ");
for (int j = 1; j <= n - i + 1; j++)
System.out.print(j);
for (int j = 1; j <= n - i; j++)
System.out.print(n - i + 1 - j);
System.out.println();
}
}
}Inverted Palindrome Number Pyramid in Python
n = 5
for i in range(1, n + 1):
for j in range(1, i):
print(" ", end="")
for j in range(1, n - i + 2):
print(j, end="")
for j in range(1, n - i + 1):
print(n - i + 1 - j, end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(nΒ²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How do I get the full diamond?
Print the palindrome number pyramid first, then this pattern starting from its second row.
What happens for n = 10?
The peak becomes 10, which has two digits, and the rows no longer line up. Keep n at 9 or less, or print each number with a space.
How do I let the user choose the number of rows?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






