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

Inverted Repeated Number Triangle (55555, 4444, 333)

The inverted repeated number triangle prints 55555 first and then 4444, 333, 22 and 1. Each row repeats one number, and that number is also the length of the row.

Quick answer: Row i prints the value n βˆ’ i + 1, and it prints it n βˆ’ i + 1 times. Because the value and the count are the same number, one expression controls both the shape and the digits.

This program is part of the Number Patterns collection.

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

Inverted Repeated Number Triangle output

Output (n = 5):

55555
4444
333
22
1
Inverted Repeated Number Triangle layout diagram, row by row (n = 5)
How each row of the inverted repeated number triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Inverted Descending Number Triangle, Inverted 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 repeated number triangle works

This is the repeated number triangle (1, 22, 333) upside down. In both versions a row shows its own length in digits, which makes the output easy to check by eye: the row of 3s has three of them. The program only needs the row length, worked out once per row.

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

Key takeaways

  • The digit on each row is also how many times it appears.
  • The row length n βˆ’ i + 1 shrinks the triangle from 5 down to 1.
  • The inner loop does not need j in the value at all.
  • Reversing the outer loop gives the upright 1, 22, 333 version.

Inverted 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 <= n - i + 1; j++)
            printf("%d", n - i + 1);
        printf("\n");
    }
    return 0;
}

Inverted 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 <= n - i + 1; j++)
            cout << n - i + 1;
        cout << endl;
    }
    return 0;
}

Inverted Repeated Number Triangle in Java

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

Inverted Repeated Number Triangle in Python

n = 5

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

How do I keep the digits but make the rows grow (5, 44, 333)?

Print n βˆ’ i + 1 but only i times. The result is the descending repeated number triangle.

Can I store the row value in a variable?

Yes: int k = n βˆ’ i + 1; at the start of the row, then print k k times. It reads more clearly and avoids working out the same value inside the inner loop.

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.

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