Shape Rule
Repeat i, i times
Row with outer i = 3 prints 333 — the digit equals the row number, repeated that many times.

Program 9 prints a repeating number triangle: each row repeats the row digit i exactly i times — 1, 22, 333, and so on. This tutorial covers the shape rule, outer loop for the digit, inner loop for repetition, a live preview, worked C examples, edge cases, and complexity.
Repeat i, i times
Row with outer i = 3 prints 333 — the digit equals the row number, repeated that many times.
i = 1..rows
for (i = 1; i <= rows; i++) — chooses which digit to repeat on each row.
j = 1..i
for (j = 1; j <= i; j++) printf("%d", i) — repeats the digit i times.
Same line / next line
Digits use printf("%d", i); end each row with printf("\n").
rows = 3..9
Pick row count and draw the repeating triangle in the browser.
Complexity
Total prints = 1+2+…+n = n(n+1)/2 — a triangular number.
A repeating number triangle prints the row digit multiple times: row 1 prints 1 once, row 2 prints 2 twice, row 3 prints 3 three times, and so on. With rows = 5, you get 1, 22, 333, 4444, 55555.
In C use an outer loop from 1 to rows, an inner loop that runs i times printing i each time, then printf("\n") after each row.
It teaches that the inner loop controls repetition count while the outer loop picks the value — a stepping stone to repeating stars and alphabets.
i is both row number and print value.
Inner runs i times, prints i.
Program 8 prints descending sequences; Program 9 repeats one digit.
Follow Program 8; continue to Program 10 next.
In short: outer i = 1..rows, inner j = 1..i, printf("%d", i) each time, then printf("\n").
Given row count rows = 5, print a repeating number triangle — row i shows digit i repeated i times.
// rows = 5
//1
//22
//333
//4444
//55555 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — also the widest row digit count. |
i (outer) | int | Current row digit — runs 1 up to rows. |
j (inner) | int | Repetition counter — runs 1..i, prints i each time. |
| Row width | int | Row with outer i prints exactly i copies of digit i. |
| First row | int | Single digit 1 when i = 1. |
| Last row | string | Digit rows repeated rows times. |
for i from 1 to rows:
repeat i times:
print i
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | for j = 1..i print i | Standard teaching approach |
| Descending outer | for (i = rows; i >= 1; i--) | Mirror triangle variant |
| User-input rows | scanf | Flexible height |
| Compact trace | rows = 3 on paper first | Quick dry-runs |
| Spaced output | printf("%d ", i) | Readable columns |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Inner loop | for (j = 1; j <= i; j++) printf("%d", i); |
| End row | printf("\n"); |
| Program 5 contrast | Program 5: print j (1..i); Program 9: print i (repeat i times) |
Same repeating triangle — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
scanfRead row count from console
rows = 3Quick dry-run on paper
i = 1..rowsChooses digit to repeat
j = 1..iRepetition count
Reach for this pattern when teaching inner-loop repetition and comparing print value vs loop counter.
Simpler than descending sequences — one digit repeated per row builds repetition intuition.
Same inner-loop repetition idea extends to repeating * or letters.
Classic nested-loop question — explain outer picks value, inner controls count.
Compare this ascending repeat with the descending repeat in Program 10.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in value-vs-counter thinking and O(n²) repetition.
Choose a row count between 3 and 9 and draw the repeating number triangle in the browser.
Three complete C programs — fixed rows, user input, and a compact trace with rows = 3. Click View Output to reveal sample console results.
Print five rows of the repeating number triangle with nested loops.
rows = 5Hard-coded height — outer picks digit, inner repeats it i times.
#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;
} Outer i runs 1 to 5 — inner j runs 1 to i, printing i each time.
Read row count from the user with validation.
Configurable height with scanf and a positive-rows check.
#include <stdio.h>
int main() {
int rows;
int i, j;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
printf("%d", i);
printf("\n");
}
return 0;
} Same nested loops — only the row count comes from console input with safe parsing.
Use rows = 3 for a quick paper trace before larger triangles.
rows = 3 TraceSmall triangle — easy to dry-run on paper before scaling up.
#include <stdio.h>
int main() {
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
printf("%d", i);
printf("\n");
}
return 0;
} Three rows, six total digits — trace i and inner count on paper before coding rows = 5.
int rows = 5; sets how many rows to print.
for (i = 1; i <= rows; i++) picks which digit to repeat on each row.
for (j = 1; j <= i; j++) prints i exactly i times per row.
printf("\n") moves to the next row after each line is printed.
Total printed digits follow triangular numbers: n(n+1)/2, so time complexity is O(n²).
rows = 5Trace each row — outer i is the digit, inner j counts repetitions from 1 to i.
| Row (i) | Inner runs | Output line |
|---|---|---|
| 1 | 1 time | 1 |
| 2 | 2 times | 22 |
| 3 | 3 times | 333 |
| 4 | 4 times | 4444 |
| 5 | 5 times | 55555 |
Total digits printed: 1+2+3+4+5 = 15 = 5×6/2 — the fifth triangular number.
Where this repetition pattern shows up beyond the homework prompt.
Inner loop count equals print value — clearest introduction to repeat-N-times logic.
Example: trace row 3 and watch i print three times.
Program 8 prints descending sequences; Program 9 repeats one digit — same O(n²) total.
Example: compare output side by side for rows = 5.
Same inner-loop repetition extends to printing * or letters per row.
Example: replace printf("%d", i) with printf("*").
Same inner bound 1..i — Program 5 prints j, Program 9 prints i.
Example: swap print value and see 123 vs 111 on row 3.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with scanf and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer picks value and inner controls count first — then write the loops.
Why this pattern earns a permanent spot in beginner C courses.
One digit repeated — wrong print value shows up immediately.
Only loops and console output — no arrays or math libraries.
Swap digits for stars, letters, or spaced output with one-line edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — row 2 should print exactly two twos.
Small habits that keep repeating-triangle code clean.
printf("%d", i) repeats the row digit — printing j gives 123 instead of 333.
Avoid crashes when the user types letters instead of a number.
Only call printf("\n") after the inner loop finishes the row.
for (j = 1; j <= i; j++) — row i gets exactly i prints.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if row 3 shows 123 instead of 333, you are printing j instead of i.
Mistakes that commonly break repeating number triangles.
Row 3 prints 123 instead of 333 — classic value-vs-counter mix-up.
→ Use printf("%d", i) inside the inner loop.
All digits print on one long line.
→ Call printf("\n") after the inner loop.
Inner running to rows instead of i over-prints each row.
→ Inner loop must be j = 1..i, not 1..rows.
Letters or empty input leave rows unread when scanf is unchecked.
→ Check scanf return value and re-prompt on failure.
Each digit prints on its own line — vertical output instead of a triangle.
→ Use printf("%d", i) inside, printf("\n") outside only.
Check these inputs before calling the solution done.
Prints only 1 — inner loop runs once.
Outer loop never runs — print nothing or show a message.
Output 1 then 22 — good quick test.
Reject with validation before the loops.
Unchecked scanf leaves rows uninitialized — check the return value.
Still O(n²) prints — cap rows for console demos.
Try these variations to lock in the pattern.
printf("%d", i) with "*"rows = 3 before codingi prints digit i exactly i times.n(n+1)/2 — triangular number. For rows = 5, that is 15 digits.1..i — only the print value differs.Quick Takeaway: outer i = 1..rows, inner j = 1..i, printf("%d", i), then printf("\n").
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Compact trace (Example 3) | O(rows²) | O(1) |
The repeating number triangle is one of the simplest nested-loop exercises: outer picks the digit, inner controls how many times it prints. Master the fixed rows = 5 version, then try user input and the compact rows = 3 trace.
Practice the three examples above, then continue to Program 10 for the descending repeating variant.
Print i not j, keep printf("\n") outside the inner loop, and validate row count when reading from the console.
for (j = 1; j <= i; j++) printf("%d", i)printf("\n") after each inner looprows > 0 for user inputrows = 3 on paper firstj when the pattern needs irows instead of iprintf("\n") inside the inner loopPrint the repeating number triangle the beginner-friendly way.
Repeat i, i times
Definitioni = 1..rows
Loopprintf("%d", i) not j
Valuej = 1..i count
RepeatO(n²) time
AnalysisEach row repeats the row number i exactly i times — outer i runs 1..rows, inner j prints i on every iteration — producing 1, 22, 333, and so on. Total prints grow as O(n²).
Move on to the descending repeating triangle in the C number-pattern series.
11 people found this page helpful