Inverted Repeating Number Triangle in C

What You’ll Learn
How to print an inverted repeating number triangle in C. Each row repeats one digit several times, and the number of repeats decreases on each next line.
The printed digit increases up to the middle row and then decreases (a mirror pattern).
⭐ Pattern Output
For rows = 5, the pattern looks like this:
11111
2222
333
22
1Complete C Program
The inner loop repeats values fewer times as i grows. A small condition decides which number to print.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = i; j <= 5; j++) {
if (i < 4) {
printf("%d", i);
} else {
printf("%d", 6 - i);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer loop controls rows
for (i = 1; i <= 5; i++) prints 5 rows.
Inner loop decides repeat count
for (j = i; j <= 5; j++) runs 5, 4, 3, 2, 1 times respectively.
Condition creates the mirror effect
Rows 1–3 print i (1, 2, 3). Rows 4–5 print 6 - i (2, 1).
Inverted repeating number triangle
Repeat count decreases each row; values mirror around the center row.
Variation — User Input Version
This version reads the row count and uses a clean mirror rule that works for any positive rows.
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int i, j;
int mid = (rows + 1) / 2;
for (i = 1; i <= rows; i++) {
int val = (i <= mid) ? i : (rows + 1 - i);
for (j = i; j <= rows; j++) {
printf("%d", val);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces between digits:
printf("%d ", val) - Validate that
rowsis positive before printing - Right-align the pattern by printing leading spaces
- Replace digits with letters for alphabet variations
- Print a full diamond by adding a second half with increasing width
Avoid
- Hardcoding the midpoint when you use user input
- Forgetting the newline after each row
- Mixing row and repeat logic (outer loop = rows, inner loop = repeats)
- Using a mirror rule that breaks for even row counts
Key Takeaways
The inner loop runs rows - i + 1 times, so each row gets shorter.
A mirror value creates the sequence 1, 2, 3, 2, 1.
Time complexity is O(n²) because total prints are n(n+1)/2.
Small conditions like this show up in many pyramid and diamond patterns.
❓ Frequently Asked Questions
i) or the decreasing half (print a mirrored value like rows + 1 - i).rows with scanf(), compute mid = (rows + 1) / 2, and mirror with rows + 1 - i for rows after the midpoint.Explore More C Number Patterns!
Practice more triangles and pyramids to master row/column thinking.
The repeat counts (5, 4, 3, 2, 1) follow a simple formula: row i prints rows - i + 1 times. That’s why the total printed digits is rows(rows+1)/2.
12 people found this page helpful
