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
- Inverted Descending Number Triangle output
- How the inverted descending number triangle works
- Inverted Descending Number Triangle in C
- Inverted Descending Number Triangle in C++
- Inverted Descending Number Triangle in Java
- Inverted Descending Number Triangle in Python
- Time and space complexity
- Frequently asked questions
Inverted Descending Number Triangle output
Output (n = 5):
54321 4321 321 21 1

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






