Shape Rule
Mirror + stars
Row 1 prints 1234554321, row 2 prints 1234**4321, row 5 prints 1********1.

The number & asterisk mirror pattern prints 1..i, a growing ** center, then i..1 — a natural step after odd-length rows in Program 22. This tutorial covers the shape rule, three inner loops, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
Mirror + stars
Row 1 prints 1234554321, row 2 prints 1234**4321, row 5 prints 1********1.
n..1
for (i = n; i >= 1; i--) shrinks the digit range each row.
j, k, m
Ascending 1..i, star pairs **, descending i..1.
Same line / next line
Digits and stars use printf; end each row with printf("\n").
1–9 size
Pick a size n and draw the mirror pattern instantly in the browser.
Complexity
Each row prints O(n) characters; total work scales as n².
A number & asterisk mirror pattern prints ascending digits, a growing star center, then descending digits on each row. With n = 5, the output is 1234554321, 1234**4321, 123****321, 12******21, 1********1.
In C you use a descending outer loop, three inner loops for j, k, and m, then printf("\n") ends each row.
It combines three inner loops with symmetry — a step up from Program 22’s single inner loop.
First inner loop prints ascending digits.
for (k = i; k < n; k++) prints star pairs.
Third loop mirrors the left half descending.
Follow Program 22; continue to Program 24 (centered pyramid) next.
In short: for each i from n down to 1, print 1..i, then ** pairs, then i..1, then printf("\n").
Given a positive integer n, print a mirror pattern: for each i from n down to 1, print digits 1..i, then (n - i) pairs of **, then digits i..1.
// n = 5 (conceptual shape)
// 1234554321
// 1234**4321
// 123****321
// 12******21
// 1********1 | Item | Type | Description |
|---|---|---|
n | int | Pattern size — outer loop runs from n down to 1. |
j | int | Ascending loop — prints 1..i. |
k | int | Star loop — prints ** for k = i..n-1. |
m | int | Descending loop — prints i..1 to mirror the left. |
for i from n down to 1:
for j from 1 to i:
print j
for k from i to n - 1:
print "**"
for m from i down to 1:
print m
print newline | Approach | Idea | Best for |
|---|---|---|
| Three inner loops | 1234554321, 1234**4321, … | Learning and interviews |
| User-input n | scanf("%d", &n); | Flexible console programs |
| Custom fill | "##" or " " instead of "**" | Different center symbols |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = n; i >= 1; i--) |
| Ascending digits | for (j = 1; j <= i; j++) printf("%d", j); |
| Star center | for (k = i; k < n; k++) printf("**"); |
| Descending digits | for (m = i; m >= 1; m--) printf("%d", m); |
| End the row | printf("\n"); |
| User input | scanf("%d", &n); |
Same mirror pattern — different ways to control size and center symbol.
1..iAscending digits in first inner loop
**Star pairs grow as i shrinks
i..1Descending digits mirror the left
3 loopsj ascending, k stars, m descending
Reach for this pattern when teaching symmetry, multiple inner loops, and mixed character output in nested loops.
Natural follow-up after Program 22 — introduces three inner loops and symmetry.
Outer/inner bound practice with an immediate visual check.
Combine loops with scanf for a flexible row count.
Compare Program 22 (odd-length rows) and Program 24 (centered pyramid) 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 size between 1 and 9 and draw the number & asterisk mirror pattern in the browser.
Three complete C programs — fixed size, user input, and custom center fill. Click View Output to reveal sample console results.
Print five rows of the mirror pattern with three inner loops.
n = 5Hard-coded size — ideal for first demos and screenshots.
#include <stdio.h>
int main() {
int i, j, k, m;
for (i = 5; i >= 1; --i) {
for (j = 1; j <= i; ++j)
printf("%d", j);
for (k = i; k < 5; ++k)
printf("**");
for (m = i; m >= 1; --m)
printf("%d", m);
printf("\n");
}
return 0;
} When i = 5, print 12345, no stars, then 54321 — full mirror with no center fill. When i = 3, print 123, two ** pairs, then 321 — output 123****321. printf("\n") after all three inner loops starts the next row.
Read the pattern size with scanf instead of hard-coding 5.
Read n with scanf("%d", &n); all three loops use n as the bound.
#include <stdio.h>
int main() {
int n;
int i, j, k, m;
printf("Enter n: ");
scanf("%d", &n);
for (i = n; i >= 1; --i) {
for (j = 1; j <= i; ++j)
printf("%d", j);
for (k = i; k < n; ++k)
printf("**");
for (m = i; m >= 1; --m)
printf("%d", m);
printf("\n");
}
return 0;
} Same three-loop core as Example 1; only the source of n changes. The star loop bound k < n scales with the user’s input. Non-numeric input leaves n unset if you ignore scanf’s return value — always check it in safer labs.
Replace ** with another two-character fill string.
##Keep n = 5 but use hash pairs instead of asterisks in the center.
#include <stdio.h>
int main() {
int n = 5;
int i, j, k, m;
for (i = n; i >= 1; --i) {
for (j = 1; j <= i; ++j)
printf("%d", j);
for (k = i; k < n; ++k)
printf("##");
for (m = i; m >= 1; --m)
printf("%d", m);
printf("\n");
}
return 0;
} Replace only "**" with "##" in the star loop — digit loops stay the same. Any two-character string works as center fill.
#include <stdio.h> brings in printf / scanf. Set n and loop variables i, j, k, m.
for (i = n; i >= 1; i--) — each row prints fewer digits and more stars.
for (j = 1; j <= i; j++) then printf("%d", j) — left half.
for (k = i; k < n; k++) then printf("**") — growing center.
for (m = i; m >= 1; m--) then printf("%d", m) — right mirror.
printf("\n") ends the row after all three inner loops.
Each row stays symmetric — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, star count, and the full row output.
i | Left 1..i | Star pairs | Right i..1 | Row output |
|---|---|---|---|---|
5 | 12345 | 0 | 54321 | 1234554321 |
4 | 1234 | 1 (× **) | 4321 | 1234**4321 |
3 | 123 | 2 | 321 | 123****321 |
2 | 12 | 3 | 21 | 12******21 |
1 | 1 | 4 | 1 | 1********1 |
Star pairs per row = n - i — grows as digits shrink.
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 max <= 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: trace all three inner loops on paper for n = 3 before coding — symmetry bugs hide in loop bounds.
Small habits that keep number-pattern code clean.
Do not skip the descending m loop — without it you lose the mirror.
scanfCheck the return value so bad input does not leave n uninitialized.
Only call printf("\n") after the inner loop finishes the row.
Write each i, star count, and mirror half before coding.
Trace n = 3 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 mirror number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use printf("%d", j) and printf("**"); printf("\n") only after all three inner loops.
Without for (m = i; m >= 1; m--) the row is not mirrored.
→ Always print i..1 after the star loop.
Using k <= n prints one extra star pair per row.
→ Use for (k = i; k < n; k++) — exactly n - i pairs.
Omitting printf("\n") glues every number onto one endless line.
→ Always end the row after the inner loop.
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 11 — one digit each side, no stars.
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.
Two rows: 1221 and 1**1.
Try these variations to lock in the pattern.
i += 2** with * — slower center growth1..i + right i..1 with star fill — row width stays consistent.printf stays on the line; printf("\n") advances — mix them carefully.n > 0 for interactive programs; n = 1 prints 11.Quick Takeaway: descending outer loop, three inner loops (j, k, m), then printf("\n") after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Custom fill (Example 3) | O(n²) | O(1) |
The number & asterisk mirror pattern is a compact lesson in symmetry: print 1..i, star pairs, then i..1 with three inner loops. Master the fixed-n version, then try user input and a custom center fill.
Practice the three examples above, then continue to Program 24 for the centered continuous number pyramid.
Never skip the descending m loop — validate n when reading from the console.
for (i = n; i >= 1; i--) in the outer loopj, k, m** in the star loop — two chars per iterationscanf return value before using nprintf("\n") inside any inner loopm loopk <= n in the star loopn in the star bound — hard-code 5 in Example 2 style only for demosn = 1 edge casePrint the pattern the beginner-friendly way.
Mirror + **
Definitionn down to 1
Codej, k, m
Coden - i pairs
ShapeO(n²) time
AnalysisEach row prints 1..i, then a growing block of ** pairs, then i..1 — three inner loops create a symmetric mirror. As i shrinks, the star block grows to keep row width consistent.
Move on to the centered continuous number pyramid in the C number-pattern series.
12 people found this page helpful