Inverted Repeating Number Triangle in C

What You’ll Learn
How to print an inverted repeating number triangle in C. Each row repeats the same digit, starting from the maximum and decreasing down to 1, while the number of repeats also shrinks.
This is the “inverted” version of the repeating triangle (Program 9): instead of growing rows, rows become shorter each line.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
55555
4444
333
22
1Complete C Program
The outer loop chooses the digit (i) and the inner loop prints it i times.
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d", i);
}
printf("\n");
}
return 0;
}🧠 How It Works
Set rows
rows controls the height and the starting digit.
Outer loop (digit + length)
i goes from rows down to 1. This sets both the printed digit and the number of repeats on that row.
Inner loop (repeat i)
j runs from 1 to i, printing i exactly i times.
New line
printf("\n") moves to the next row.
Inverted repeating triangle
Total prints are 1+2+…+n = n(n+1)/2, so runtime is O(n²).
Variation — User Input Version
Accept the number of rows from the user using scanf():
#include <stdio.h>
int main() {
int rows;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d", i);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces with
printf("%d ", i) - Flip the outer loop direction to get the ascending repeating triangle (Program 9)
- Use letters for AAAAA, BBBB, CCC... style patterns
- Right-align by printing leading spaces before each row
Avoid
- Hard-coding the row count in your loops
- Forgetting the newline after each row
- Not validating user input when using
scanf()
Key Takeaways
Row i repeats digit i exactly i times.
The triangle is inverted because the outer loop counts down, shrinking row lengths.
Total prints are n(n+1)/2, so runtime is O(n²).
This is the inverted version of the repeating triangle (Program 9).
❓ Frequently Asked Questions
i = rows, and the inner loop runs i times, so the first row prints the maximum number of digits.rows and keep printing i inside the inner loop.Explore More Pattern Variations
Try combining repetition with alignment and spacing to create inverted pyramids and mirrored triangles.
This is the “true inverted” version of the repeating triangle: instead of controlling repetition with a decreasing inner-loop range, you directly run the inner loop from 1 to i while i itself counts down.
12 people found this page helpful
