Shape Rule
Sequence + fill
Row 1 prints 5 5 5 5 5, row 2 prints 4 5 5 5 5, row 3 prints 3 4 5 5 5, and so on.

The fill-with-5 number triangle pads each row with the maximum value so every line has width n — a natural step after alternating odd/even patterns. This tutorial covers the shape rule, two inner loops, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
Sequence + fill
Row 1 prints 5 5 5 5 5, row 2 prints 4 5 5 5 5, row 3 prints 3 4 5 5 5, and so on.
n..1
for (i = n; i >= 1; i--) walks rows from the top (all fill) down to the full sequence.
i..n then pad
for (j = i; j <= n; j++) prints the sequence; for (j = 1; j < i; j++) fills with n.
Same line / next line
Numbers use printf("%d ", j) or printf("%d ", n); end each row with printf("\n").
1–15 width
Pick a triangle width and draw the fill-with-n pattern instantly in the browser.
Complexity
Each of n rows prints n numbers — total prints = n²; extra memory stays O(1).
A fill-with-5 number triangle prints an ascending sequence on each row, then pads the rest with the maximum value so every row has the same width. With n = 5, the output is 5 5 5 5 5, 4 5 5 5 5, 3 4 5 5 5, 2 3 4 5 5, 1 2 3 4 5.
In C you use a descending outer loop, print j from i to n in the first inner loop, fill remaining slots with n in the second inner loop, then printf("\n") ends each row.
It combines two inner loops with fixed row width — a step up from Program 18.
for (j = i; j <= n; j++) prints ascending numbers.
for (j = 1; j < i; j++) pads with n.
printf("%d ", j) or printf("%d ", n) in inner loops; printf("\n") after.
Follow Program 18; continue to Program 20 (continuous number triangle).
In short: for each i from n down to 1, print j from i to n, fill i - 1 times with n, then call printf("\n").
Given a positive integer n, print a fill-with-n triangle: each row prints an ascending sequence from i to n, then pads with n so every row has width n.
// n = 5 (conceptual shape)
// 5 5 5 5 5
// 4 5 5 5 5
// 3 4 5 5 5
// 2 3 4 5 5
// 1 2 3 4 5 | Item | Type | Description |
|---|---|---|
n | int | Triangle width and fill value (typically ≥ 1). |
| Printed output | text | Each row has n spaced numbers — sequence then padding. |
for i from n down to 1:
for j from i to n:
print j + space
for j from 1 to i - 1:
print n + space
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | 5 5 5 5 5, 4 5 5 5 5, … | Learning and interviews |
Variable n | scanf("%d", &n); | User-input version |
| Custom fill | Separate fill constant | Pad with a value other than n |
| Goal | Pattern |
|---|---|
| Walk each row | for (i = n; i >= 1; i--) |
| Print sequence | for (j = i; j <= n; j++) printf("%d ", j); |
| Fill padding | for (j = 1; j < i; j++) printf("%d ", n); |
| End the row | printf("\n"); |
| User input | scanf("%d", &n); |
| Custom fill value | printf("%d ", fill); in second loop |
Same fill-with-n triangle — different ways to structure the padding.
j = i..nFirst inner loop prints ascending numbers
pad nSecond loop runs i - 1 times with n
inputReplace hard-coded 5 with user input in Example 2
width nEvery row must print exactly n numbers
Reach for this pattern when teaching two inner loops and fixed-width row padding.
Natural follow-up after Program 18 — combines sequence printing with right padding.
Outer/inner bound practice with an immediate visual check.
Combine loops with scanf for a flexible row count.
Compare Program 18 (alternating odd/even) and Program 20 (continuous counter triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a triangle width between 1 and 15 and draw the fill-with-n pattern in the browser.
Three complete C programs — fixed width, user input, and custom fill constant. Click View Output to reveal sample console results.
Print five rows of the fill-with-5 triangle with two inner loops.
n = 5Hard-coded width — ideal for first demos and screenshots.
#include <stdio.h>
int main() {
int n = 5;
int i, j;
for (i = n; i >= 1; --i) {
for (j = i; j <= n; ++j)
printf("%d ", j);
for (j = 1; j < i; ++j)
printf("%d ", n);
printf("\n");
}
return 0;
} When i = 5, the sequence loop prints 5 once, then the fill loop runs 4 times — all 5s. When i = 3, the sequence prints 3 4 5, then two 5s pad the row. When i = 1, the sequence prints 1 2 3 4 5 with no fill needed. printf("\n") after both inner loops starts the next row.
Read the triangle width with scanf instead of hard-coding 5.
Read n with scanf("%d", &n); the fill value matches the width.
#include <stdio.h>
int main() {
int n;
int i, j;
printf("Enter the triangle width: ");
scanf("%d", &n);
for (i = n; i >= 1; --i) {
for (j = i; j <= n; ++j)
printf("%d ", j);
for (j = 1; j < i; ++j)
printf("%d ", n);
printf("\n");
}
return 0;
} Same nested-loop core as Example 1; only the source of n changes. Both the sequence end bound and the fill value use the same variable. Non-numeric input leaves n unset if you ignore scanf’s return value — always check it in safer labs.
Use a separate fill constant instead of always padding with n.
Pad with fill = 9 while the sequence still runs up to n = 5.
#include <stdio.h>
int main() {
int n = 5;
int fill = 9;
int i, j;
for (i = n; i >= 1; --i) {
for (j = i; j <= n; ++j)
printf("%d ", j);
for (j = 1; j < i; ++j)
printf("%d ", fill);
printf("\n");
}
return 0;
} Replace n with fill in the second inner loop only. The sequence loop still prints j from i to n; padding uses the custom constant.
#include <stdio.h> brings in printf / scanf. Set n (fixed or from input).
for (i = n; i >= 1; i--) walks from the all-fill top row down to the full sequence.
Print j from i to n, then pad i - 1 times with n (or a custom fill value).
printf("\n") ends the row so the next outer iteration starts fresh.
Total prints: n² — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, the sequence printed, the fill count, and the final row.
i | Sequence (j = i..n) | Fill count (i - 1) | Row output |
|---|---|---|---|
5 | 5 | 4 | 5 5 5 5 5 |
4 | 4, 5 | 3 | 4 5 5 5 5 |
3 | 3, 4, 5 | 2 | 3 4 5 5 5 |
2 | 2, 3, 4, 5 | 1 | 2 3 4 5 5 |
1 | 1, 2, 3, 4, 5 | 0 | 1 2 3 4 5 |
Total number prints: 5 + 5 + 5 + 5 + 5 = 25 = 5².
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice printf vs row newline without complex math.
Example: put printf("\n") inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with scanf return checks and positive-row checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the sequence loop first, then add the fill loop — compare with custom fill in Example 3.
Small habits that keep number-pattern code clean.
Use n for both width and fill value unless you need a custom constant.
scanfCheck the return value so bad input does not leave n uninitialized.
Only call printf("\n") after the inner loop finishes the row.
Write row i, sequence j = i..n, and fill count i - 1 before coding.
Trace n = 5 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put printf("\n") inside the inner loop.
Mistakes that commonly break fill-with-n number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use printf("%d ", j) or printf("%d ", n); printf("\n") only after both inner loops.
Rows have different widths — the top row may be short while the bottom is full.
→ Add for (j = 1; j < i; j++) to pad with n after the sequence loop.
j <= i in the fill loop prints too many padding values.
→ Use j < i so the fill runs exactly i - 1 times.
Omitting printf("\n") glues every number onto one endless line.
→ Always end the row after both inner loops.
Letters or empty input leave n uninitialized.
→ Check scanf return value and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no fill needed.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
Unchecked scanf leaves n unset — check the return value.
Sequence must run j = i to n, not j = 1 to i.
Without the fill loop, top rows are shorter than the bottom row.
Try these variations to lock in the pattern.
i % 2 and k += 2nn² — each of n rows prints n numbers.printf("%d ", j) stays on the line; printf("\n") advances — mix them carefully.n > 0 for interactive programs; n = 1 should print a single 1.Quick Takeaway: descending outer loop, print sequence j = i..n, fill i - 1 times with n, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Custom fill (Example 3) | O(n²) | O(1) |
The fill-with-5 number triangle is a compact lesson in two inner loops: the first prints an ascending sequence, the second pads with the maximum value so every row has width n. Master the fixed-n version, then try user input and a custom fill constant.
Practice the three examples above, then continue to Program 20 for the continuous number triangle.
Run the sequence loop first, then the fill loop — use j < i for padding and validate n when reading input.
printf("%d ", j) and printf("%d ", n)n ≥ 1 for interactive programsscanf return value before using nprintf("\n") inside the inner digit loopj <= i in the fill loopn = 1 edge casePrint the pattern the beginner-friendly way.
Sequence then fill
Definitioni = n..1
j = i..n
Pad i - 1 times
O(n²) time
AnalysisEach row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total prints.
Move on to the continuous number triangle in the C number-pattern series.
12 people found this page helpful