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

Inverted Descending Number Triangle (54321, 4321, 321)

The inverted descending number triangle starts with 54321 and drops the largest number on each row, so the rows read 54321, 4321, 321, 21 and finally 1.

Quick answer: Row i prints n βˆ’ i + 1 numbers, which makes the triangle shrink. The value at position j is n βˆ’ i + 2 βˆ’ j: it starts at the row length and counts down to 1. For n = 5, row 2 prints 4, 3, 2, 1.

This program is part of the Number Patterns collection.

Table of contents8 sections
  1. Inverted Descending Number Triangle output
  2. How the inverted descending number triangle works
  3. Inverted Descending Number Triangle in C
  4. Inverted Descending Number Triangle in C++
  5. Inverted Descending Number Triangle in Java
  6. Inverted Descending Number Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Inverted Descending Number Triangle output

Output (n = 5):

54321
4321
321
21
1
Inverted Descending Number Triangle layout diagram, row by row (n = 5)
How each row of the inverted descending number triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Inverted Number Triangle, Inverted Repeated Number Triangle, Inverted Reverse Number Triangle, Inverted Right Triangle Star Pattern and Suffix Number Triangle programs draw 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 descending number triangle works

Turning a triangle upside down only changes the row length: n βˆ’ i + 1 instead of i. Each row then counts down from its own length to 1. The value formula follows from that: the first number of row i is n βˆ’ i + 1, and every step to the right takes one off, which gives n βˆ’ i + 2 βˆ’ j.

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

Key takeaways

  • The row length n βˆ’ i + 1 makes the triangle shrink.
  • Each row counts down from its length to 1.
  • The last column is all 1s, and the first column reads 5, 4, 3, 2, 1.
  • It is the descending number triangle printed in reverse row order.

Inverted Descending Number Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++)
            printf("%d", n - i + 2 - j);
        printf("\n");
    }
    return 0;
}

Inverted Descending Number Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++)
            cout << n - i + 2 - j;
        cout << endl;
    }
    return 0;
}

Inverted Descending Number Triangle in Java

public class InvertedDescendingNumberTriangle {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i + 1; j++)
                System.out.print(n - i + 2 - j);
            System.out.println();
        }
    }
}

Inverted Descending Number Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, n - i + 2):
        print(n - i + 2 - 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

Can I get the same output by running the rows backwards?

Yes. Take the descending triangle (1, 21, 321) and run its outer loop from n down to 1. Row i then prints i down to 1, which is the same output with a simpler formula.

How do I make the rows count up instead (12345, 1234, 123)?

Print j instead of the formula. That gives the inverted number triangle.

Why does the formula have n βˆ’ i + 2?

Because the first value on the row must be n βˆ’ i + 1 when j = 1. Adding 1 more and then subtracting j gives exactly that at the start and 1 at the end.

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 14 of 24 in Number 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.