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

Descending Number Triangle (1, 21, 321)

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
  1. Descending Number Triangle output
  2. How the descending number triangle works
  3. Descending Number Triangle in C
  4. Descending Number Triangle in C++
  5. Descending Number Triangle in Java
  6. Descending Number Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Descending Number Triangle output

Output (n = 5):

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

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.

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

Key takeaways

  • Row i starts at its own number i and counts down to 1.
  • i − j + 1 turns 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.

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