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

Inverted Number Pyramid

The inverted number pyramid is the number pyramid turned upside down: the widest row, 1 2 3 4 5, comes first and each row below is one number shorter and one space further in.

Quick answer: Row i starts with i βˆ’ 1 spaces and then prints 1 to n βˆ’ i + 1, each number followed by a space. The indent grows by one while the row loses one number, and because each number takes two characters the rows stay centred.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Output
  2. How the inverted number pyramid works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Inverted Number Pyramid output

Output (n = 5):

1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1
Inverted Number Pyramid layout diagram, row by row (n = 5)
How each row of the inverted number pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Inverted Alphabet Pyramid 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 number pyramid works

In a centred pattern, every number with its space is two characters wide, so dropping one number makes the row two characters shorter. Moving the start one space to the right takes one character off the left, and the missing number takes the other one off the right. That is why one extra space per row is enough to keep the triangle symmetrical.

  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. Move to the next line.

Key takeaways

  • The indent is i βˆ’ 1 spaces and grows by one each row.
  • Each row prints 1 to n βˆ’ i + 1, one number fewer than the row above.
  • A number plus its trailing space is two characters, which is what keeps the rows centred.
  • Print it after a normal number pyramid and you get a number diamond.

Inverted 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);
        printf("\n");
    }
    return 0;
}

Inverted 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 << " ";
        cout << endl;
    }
    return 0;
}

Inverted Number Pyramid in Java

public class InvertedNumberPyramid {
    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 + " ");
            System.out.println();
        }
    }
}

Inverted 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=" ")
    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 combine it with the number pyramid?

Print the number pyramid first, then this pattern starting from row 2 so the widest row is not printed twice. The result is a diamond of numbers.

Can every row count down instead?

Yes. Print n βˆ’ i + 2 βˆ’ j instead of j. The first row then reads 5 4 3 2 1.

Why does the pattern lean for n = 12?

From 10 on, a number with its space takes three characters, but the indent still grows by one. Print each number as %3d and indent by two spaces per row (half the width plus a bit) to fix it, or keep n below 10.

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 9 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.