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
- Inverted Repeated Number Triangle output
- How the inverted repeated number triangle works
- Inverted Repeated Number Triangle in C
- Inverted Repeated Number Triangle in C++
- Inverted Repeated Number Triangle in Java
- Inverted Repeated Number Triangle in Python
- Time and space complexity
- Frequently asked questions
Inverted Repeated Number Triangle output
Output (n = 5):
55555 4444 333 22 1

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n β i + 1numbers (valuen β i + 1). - Move to the next line.
Key takeaways
- The digit on each row is also how many times it appears.
- The row length
n β i + 1shrinks the triangle from 5 down to 1. - The inner loop does not need
jin 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.






