Repeating Number Triangle in C

What You’ll Learn
How to print a repeating number triangle in C. Each row repeats the row number: row 1 prints one 1, row 2 prints two 2s, and so on.
The important trick is printing i (row index) inside the inner loop, instead of printing j.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
22
333
4444
55555Complete C Program
The inner loop controls how many times to print, while printf("%d", i) prints the repeating digit for that row.
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d", i);
}
printf("\n");
}
return 0;
}🧠 How It Works
Setup
rows = 5 sets how many lines to print.
Outer loop (row number)
i goes from 1 to rows. This value decides which digit will repeat on the row.
Inner loop (repeat count)
j runs from 1 to i, so the digit is printed exactly i times.
New line
printf("\n") moves to the next row.
Repeating triangle
Total prints are 1+2+…+n = n(n+1)/2, so time complexity 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 = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d", i);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Print
jinstead ofifor an ascending triangle - Add spaces with
printf("%d ", i) - Use characters instead of digits for repeating alphabet triangles
- Try a hollow version by printing only borders
Avoid
- Printing
jif your goal is repetition (it changes the pattern) - Forgetting the newline after each row
- Not validating user input
Key Takeaways
Row i prints the same digit because you print i inside the inner loop.
The inner loop runs i times, creating the triangle growth.
Total prints are n(n+1)/2, so runtime is O(n²).
Switching from printing i to j gives a completely different triangle.
❓ Frequently Asked Questions
i (which is 2) each time.j instead of i: use printf("%d", j).Explore More Pattern Variations
Repeating patterns are a great stepping stone to Floyd’s triangle and other numeric layouts.
Repeating patterns are often the simplest way to test whether you really understand the difference between row index (i) and column index (j).
12 people found this page helpful
