New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Inverted Palindrome Number Pyramid (123454321, 1234321)

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
  1. Inverted Palindrome Number Pyramid output
  2. How the inverted palindrome number pyramid works
  3. Inverted Palindrome Number Pyramid in C
  4. Inverted Palindrome Number Pyramid in C++
  5. Inverted Palindrome Number Pyramid in Java
  6. Inverted Palindrome Number Pyramid in Python
  7. Time and space complexity
  8. Frequently asked questions

Inverted Palindrome Number Pyramid output

Output (n = 5):

123454321
 1234321
  12321
   121
    1
Inverted Palindrome Number Pyramid layout diagram, row by row (n = 5)
How each row of the inverted palindrome number pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print i βˆ’ 1 spaces.
  3. Print n βˆ’ i + 1 numbers (value j).
  4. Print n βˆ’ i numbers (value n βˆ’ i + 1 βˆ’ j).
  5. Move to the next line.

Key takeaways

  • The peak k = n βˆ’ i + 1 falls 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 18 of 19 in Pyramid Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.