Shape Rule
Diagonal star
Each row prints descending digits with one * where i == j — the star moves left each row.

The descending number pattern with diagonal asterisk prints 5432*, 543*1, 54*21, 5*321, *4321 — a natural step after the bidirectional triangle in Program 25. This tutorial covers descending digits, the i == j condition, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
Diagonal star
Each row prints descending digits with one * where i == j — the star moves left each row.
i = 1..n
for (i = 1; i <= n; i++) walks each row top to bottom.
j = n..1
for (j = n; j >= 1; j--) prints digits 5, 4, 3, 2, 1 per row.
i == j
When i == j, print *; otherwise print j.
3–9 size
Pick a size n and draw the diagonal asterisk pattern instantly in the browser.
Complexity
Each row prints n characters; total work scales as n².
A descending number pattern with diagonal asterisk prints digits from n down to 1 on each row, replacing one position with * where i == j. With n = 5, the output is 5432*, 543*1, 54*21, 5*321, *4321.
In C you use an outer loop for rows, a descending inner loop for columns, and an if (i == j) to swap a digit for a star.
It combines row/column indexing with a conditional swap — a step up from Program 25’s digit mapping.
j = n..1 — digits decrease left to right.
i == j marks the star position.
As i grows, the star shifts left each row.
Follow Program 25; continue to Program 27 (palindrome triangle) next.
In short: for each i, scan j from n down to 1 — print * when i == j, else print j, then printf("\n").
Given a positive integer n (e.g. 5), print n rows of descending digits with one diagonal * per row where i == j.
// n = 5 (conceptual shape)
// 5432*
// 543*1
// 54*21
// 5*321
// *4321 | Item | Type | Description |
|---|---|---|
n | int | Pattern size — outer loop runs from 1 to n. |
i | int | Outer loop — current row number; also the diagonal star column. |
j | int | Inner loop — descending column digit from n down to 1. |
| Output | char | * when i == j; otherwise the digit j. |
for i from 1 to n:
for j from n down to 1:
if i == j:
print "*"
else:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| if (i == j) | 5432*, 543*1, … | Learning and interviews |
| Custom symbol | Replace * with # or any char | Visual variants |
| User-input n | scanf("%d", &n); | Flexible console programs |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= n; i++) |
| Descending columns | for (j = n; j >= 1; j--) |
| Diagonal star | if (i == j) printf("*"); |
| Otherwise digit | else printf("%d", j); |
| End the row | printf("\n"); |
| User input | scanf("%d", &n); |
Same diagonal asterisk pattern — different ways to control size and the replacement character.
i = 1..nRow index doubles as star column
j = n..1Descending digits per row
i == jSwap digit for star on diagonal
if/elseOne inner loop handles star vs digit
Reach for this pattern when teaching row/column indexing, conditional character substitution, and diagonal effects in nested loops.
Natural follow-up after Program 25 — introduces i == j diagonal substitution.
Outer/inner bound practice with an immediate visual check.
Combine loops with scanf for a flexible row count.
Compare Program 25 (bidirectional triangle) and Program 27 (palindrome 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 size between 3 and 9 and draw the descending diagonal asterisk pattern in the browser.
Three complete C programs — fixed size, custom symbol, and user input. Click View Output to reveal sample console results.
Print five rows of the diagonal asterisk pattern with i == j.
n = 5Hard-coded size — ideal for first demos and screenshots.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; ++i) {
for (j = 5; j >= 1; --j) {
if (i == j)
printf("*");
else
printf("%d", j);
}
printf("\n");
}
return 0;
} When i = 1, the star lands at j = 1 (rightmost) — output 5432*. When i = 5, the star is at the leftmost position — output *4321. Each row always prints n characters.
Replace the diagonal asterisk with another character like #.
#Keep n = 5 but use hash instead of asterisk on the diagonal.
#include <stdio.h>
int main() {
int n = 5;
int i, j;
for (i = 1; i <= n; ++i) {
for (j = n; j >= 1; --j) {
if (i == j)
printf("#");
else
printf("%d", j);
}
printf("\n");
}
return 0;
} Only the replacement character changes — "#" instead of "*". Loop bounds and the i == j condition stay the same as Example 1.
Read the pattern size with scanf instead of hard-coding 5.
Read n with scanf("%d", &n); both loops use n as the bound.
#include <stdio.h>
int main() {
int n;
int i, j;
printf("Enter size: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
for (j = n; j >= 1; --j) {
if (i == j)
printf("*");
else
printf("%d", j);
}
printf("\n");
}
return 0;
} Same i == j core as Example 1; only the source of n changes. The diagonal star 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.
#include <stdio.h> brings in printf / scanf. Set loop variables i, j and n = 5.
for (i = 1; i <= n; i++) — row index also marks the star column.
for (j = n; j >= 1; j--) — prints digits n..1 per row.
if (i == j) prints *; else printf("%d", j).
printf("\n") ends the row after the inner loop.
Star moves left each row — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, where i == j, and the full row output.
i | Star at j | Digits printed | Row output |
|---|---|---|---|
1 | j = 1 | 5, 4, 3, 2, * | 5432* |
2 | j = 2 | 5, 4, 3, *, 1 | 543*1 |
3 | j = 3 | 5, 4, *, 2, 1 | 54*21 |
4 | j = 4 | 5, *, 3, 2, 1 | 5*321 |
5 | j = 5 | *, 4, 3, 2, 1 | *4321 |
The star position moves left as i increases — each row still prints exactly n characters.
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 = n..1 to j = 1..n and watch digit order flip.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use i + j == n + 1 for the anti-diagonal star.
Practice Write vs printf("\n") without complex math.
Example: put printf("\n") inside the inner loop by mistake.
Swap digits for letters or add spaces once the loop works.
Example: replace * with # or a space character.
Triangular totals make O(n²) concrete for beginners.
Example: count printed chars for n = 5 → 5 × 5 = 25.
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 i and j on paper for n = 3 before coding — the star column is where they meet.
Small habits that keep number-pattern code clean.
Do not print i in the else branch — use j for the descending digit.
scanfCheck the return value so bad input does not leave n uninitialized.
Only call printf("\n") after the inner loop finishes the row.
Mark where i == j on each row 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 diagonal asterisk patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use printf("*") or printf("%d", j); printf("\n") only after the inner loop.
Flipping the condition prints stars everywhere except the diagonal.
→ Print * when i == j, not when they differ.
Using j = 1..n reverses the digit order on each row.
→ Use for (j = n; j >= 1; j--) for descending digits.
Writing printf("%d", i) in the else branch repeats the row number, not the column digit.
→ Print j in the else branch — it holds the descending column value.
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 * — one star, one row.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 2* and *1.
Unchecked scanf leaves n unset — check the return value.
Output grows as n² characters — fine for labs, noisy for huge n.
Try these variations to lock in the pattern.
i + j == n + 1 instead of i == j* when i == j or i + j == n + 1i == j, print *; otherwise print descending digit j.printf stays on the line; printf("\n") advances — mix them carefully.n > 0 for interactive programs; n = 1 prints a single *.#, X, or a space.Quick Takeaway: outer loop i = 1..n, descending inner loop j = n..1, print * when i == j else j, then printf("\n").
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1 & 3) | O(n²) | O(1) |
| Custom symbol (Example 2) | O(n²) | O(1) |
The descending number pattern with diagonal asterisk is a compact lesson in row/column indexing: print descending j values and swap one position with * when i == j. Master the fixed-n version, then try a custom symbol and user input.
Practice the three examples above, then continue to Program 27 for the palindrome number triangle.
Print j in the else branch, not i — validate n when reading from the console.
for (i = 1; i <= n; i++) in the outer loopfor (j = n; j >= 1; j--)* when i == j, else print jscanf return value before using nprintf("\n") inside the inner loopi instead of j in the else branchi != jj unless you want reversed digitsn = 1 edge casePrint the pattern the beginner-friendly way.
i == j → *
DefinitionDescending
Code* or j
CodeLeft each row
ShapeO(n²) time
AnalysisThis pattern prints descending numbers from n to 1 on each row. When the row index equals the current column value (i == j), it prints * instead of the number, creating a diagonal asterisk that moves left each row.
Move on to the palindrome number triangle in the C number-pattern series.
12 people found this page helpful