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

Descending Repeated Number Triangle (5, 44, 333)

The descending repeated number triangle grows like a normal triangle, but its digits go down: 5, 44, 333, 2222, 11111. The longest row is made of 1s.

Quick answer: Keep the normal triangle loops, with row i printing i characters, and print the value n βˆ’ i + 1 each time. The row length goes up while the digit goes down, so the bottom row is five 1s.

This program is part of the Number Patterns collection.

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

Descending Repeated Number Triangle output

Output (n = 5):

5
44
333
2222
11111
Descending Repeated Number Triangle layout diagram, row by row (n = 5)
How each row of the descending repeated 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 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 repeated number triangle works

Two things change from row to row and they move in opposite directions. The count follows i, which is what gives the triangle its shape. The digit follows n βˆ’ i + 1, which counts down. Keeping those two ideas apart, shape from the loop and value from the formula, is the habit that makes every number pattern easier.

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

Key takeaways

  • The shape comes from the loop and the digit from the formula.
  • The count grows with i while the digit shrinks with n βˆ’ i + 1.
  • The digits on a row never change, so the inner loop ignores j.
  • Pairs well with the repeated number triangle (1, 22, 333) for practice.

Descending Repeated 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", n - i + 1);
        printf("\n");
    }
    return 0;
}

Descending Repeated 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 << n - i + 1;
        cout << endl;
    }
    return 0;
}

Descending Repeated Number Triangle in Java

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

Descending Repeated Number Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(n - i + 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

Why is the bottom row all 1s?

On the last row i = n, so the value n βˆ’ i + 1 is 1, and the row has n positions. That gives five 1s for n = 5.

How do I right-align the triangle?

Print n βˆ’ i spaces before the digits. The rows then end in the same column, like the mirrored number triangle.

What changes for n = 12?

The first rows print 12 and 11, which have two digits, so they look twice as long as intended. Add a space after each number, or keep n below 10.

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