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

Inverted Reverse Number Triangle (54321, 5432, 543)

The inverted reverse number triangle always starts at 5 and stops a little earlier on each row: 54321, 5432, 543, 54, 5. It is the reverse number triangle upside down.

Quick answer: Row i prints n βˆ’ i + 1 numbers. The number at position j is n βˆ’ j + 1, which always starts at n and counts down. Because the row gets shorter, it stops one number earlier each time.

This program is part of the Number Patterns collection.

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

Inverted Reverse Number Triangle output

Output (n = 5):

54321
5432
543
54
5
Inverted Reverse Number Triangle layout diagram, row by row (n = 5)
How each row of the inverted reverse number triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Inverted Descending Number Triangle, Inverted Number Triangle, Inverted Repeated 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 reverse number triangle works

Notice that the value does not depend on the row at all: position 1 is always 5, position 2 is always 4, and so on. The row number only decides how many positions are printed. That makes this one of the easiest inverted patterns: change the row length to n βˆ’ i + 1 and keep the countdown formula.

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

Key takeaways

  • The value n βˆ’ j + 1 depends only on the column, not the row.
  • The row length n βˆ’ i + 1 decides where each row stops.
  • Every row starts with n; the right edge reads 1, 2, 3, 4, 5 from top to bottom.
  • Reading the columns gives a column of 5s, then 4s, and so on, each one shorter.

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

Inverted Reverse 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 - j + 1;
        cout << endl;
    }
    return 0;
}

Inverted Reverse Number Triangle in Java

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

Inverted Reverse Number Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, n - i + 2):
        print(n - j + 1, 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 make each row end at 1 instead?

Print n βˆ’ i + 2 βˆ’ j: each row then counts down from its own length to 1, which gives the inverted descending number triangle.

What is the upright version?

Run the rows the other way: row i prints i numbers with the same value formula. That is the reverse number triangle: 5, 54, 543.

How do I let the user choose n?

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