The descending number triangle prints each row backwards: row 3 is 321 and row 5 is 54321. It is the number right triangle with every row read from right to left.
Quick answer: Use the usual right-triangle loops, row i printing i numbers, but print i − j + 1 instead of j. As j goes from 1 to i, that value goes from i down to 1, so every row starts with its own row number and ends with 1.
This program is part of the Number Patterns collection.
Table of contents8 sections
Descending Number Triangle output
Output (n = 5):
1 21 321 4321 54321

Same shape, other characters: the Alternating Star and Hash Triangle, Descending Repeated Number Triangle, Number Right Triangle, Repeated Number Triangle and Reverse 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 descending number triangle works
The shape is fixed by the loop counts: row i has i positions. Only the value changes. Instead of counting up with j, the program counts down from i. The expression i − j + 1 is the standard way to reverse a count: it gives i when j = 1 and 1 when j = i.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
inumbers (valuei − j + 1). - Move to the next line.
Key takeaways
- Row
istarts at its own numberiand counts down to 1. i − j + 1turns an upward count into a downward one.- Every row ends in 1, so the right edge of the triangle reads 1, 1, 1, 1, 1.
- The first column is 1, 2, 3, 4, 5: the row numbers.
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 <= i; j++)
printf("%d", i - j + 1);
printf("\n");
}
return 0;
}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 <= i; j++)
cout << i - j + 1;
cout << endl;
}
return 0;
}Descending Number Triangle in Java
public class DescendingNumberTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print(i - j + 1);
System.out.println();
}
}
}Descending Number Triangle in Python
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(i - 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 is this different from the 5, 54, 543 triangle?
The reverse number triangle always starts at n and counts down a little further each row. This one starts at the row number and always ends at 1, so row 2 is 21 here but 54 there.
How do I put spaces between the numbers?
Print a space after each number: change the format to "%d " in C or add end=" " in Python. The rows then read 3 2 1 and the triangle gets twice as wide.
How do I let the user choose the number of rows?
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.






