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
Inverted Reverse Number Triangle output
Output (n = 5):
54321 5432 543 54 5

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n β i + 1numbers (valuen β j + 1). - Move to the next line.
Key takeaways
- The value
n β j + 1depends only on the column, not the row. - The row length
n β i + 1decides 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.






