Each row prints digits from n down to 1, but when i == j it prints * instead — a diagonal asterisk that moves left each row.
Remember
Rule: for i from 1 to n
for j from n down to 1
print * if i == j, else print j
5432*
543*1
54*21
5*321
*4321 ← n = 5
Follows the bidirectional triangle in Program 25; next is the palindrome number triangle in Program 27.
Approach
How to Solve It
Outer loop grows i. Reverse inner loop prints descending j, swapping to * when i == j.
Method
Idea
Best for
i == j swap
Print * on the diagonal; otherwise print descending j
Learning, interviews, exams
Custom symbol
Replace "*" with "#" or any character
Same shape, different marker
Pseudocode
Pseudocode
for i from 1 to n:
for j from n down to 1:
if i == j:
print "*"
else:
print j
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (i = 1; i <= n; i++)
Descend columns
for (j = n; j >= 1; j--)
Diagonal star
if (i == j) cout << "*";
Else digit
else cout << j;
End of row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / "*"
Stays on the same line
Each digit or star on the row
cout << "\n"
Ends the current line
After the inner loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the size n and the diagonal asterisk pattern updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · 25 chars
5432*
543*1
54*21
5*321
*4321
Trace
Worked Walkthrough — n = 5
Trace how i == j places the star while the rest of the row stays descending digits.
i
Star at j
Sequence
Prints
1
1 (right)
5 4 3 2 *
5432*
2
2
5 4 3 * 1
543*1
3
3
5 4 * 2 1
54*21
4
4
5 * 3 2 1
5*321
5
5 (left)
* 4 3 2 1
*4321
Each of n rows prints n characters → n² prints → O(n²).
Code
C++ Programs
Three complete programs: fixed n = 5, cin input, and a compact n = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded size — descending j, print * when i == j.
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int i, j;
for (i = 1; i <= n; i++)
{
for (j = n; j >= 1; j--)
{
if (i == j)
cout << "*";
else
cout << j;
}
cout << "\n";
}
return 0;
}
Output
5432*
543*1
54*21
5*321
*4321
How It Works
1. Outer sets the star column.i is both the row index and where * appears.
2. Inner descends.j runs from n down to 1 for every row.
3. Swap on diagonal. When i == j print *; otherwise print j.
Example 2 — User Input Size
Read n with cin, validate, then use the same i == j core.
C++
#include <iostream>
using namespace std;
int main()
{
int n;
int i, j;
cout << "Enter size: ";
if (!(cin >> n) || n <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= n; i++)
{
for (j = n; j >= 1; j--)
{
if (i == j)
cout << "*";
else
cout << j;
}
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter size: 4
432*
43*1
4*21
*321
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Diagonal swap matches Example 1 — only n comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> n) || n < 1 || n > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact n = 3
Same structure with only three rows — easy to confirm the star moving left.
C++
#include <iostream>
using namespace std;
int main()
{
int n = 3;
int i, j;
for (i = 1; i <= n; i++)
{
for (j = n; j >= 1; j--)
{
if (i == j)
cout << "*";
else
cout << j;
}
cout << "\n";
}
return 0;
}
Output
32*
3*1
*21
How It Works
1. Three rows.i = 1 → 32*; i = 2 → 3*1; i = 3 → *21.
2. Trace on paper. If you flip the condition to i != j, stars appear everywhere except the diagonal.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for n = 5 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
i != j
Flipped condition
Printing * when i != j fills almost the whole grid with stars. Keep i == j for the diagonal only.
j++
Ascending inner loop
Using j = 1..n prints ascending digits. For this shape keep j = n..1.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, each character lands on its own line. Call it only after the row finishes.
n = 1
Single star
Output is just * — a good sanity check for input validation.
cout << i
Wrong digit
Printing i instead of j fills each row with the same digit. Print the column variable j.
cin
Check the stream
Validate cin >> n before looping — a failed read leaves n unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact n = 3 (Example 3)
O(n²)
O(1)
Each of n rows prints exactly n characters → n² prints → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Descend with j: inner loop runs j = n..1 every row.
Diagonal rule: print * when i == j; otherwise print j.
Break the row: digits/stars in the inner loop; cout << "\n" after it.
Complexity:O(n²) time from n² characters; O(1) extra space.
One line: for each row i, scan j from n to 1 — print * if i == j, else j, then newline.
Frequently Asked Questions
A descending digit grid with one diagonal asterisk: for n=5 you get 5432* / 543*1 / 54*21 / 5*321 / *4321 — the star moves left each row.
Because i increases from 1 to n while j decreases from n to 1. The condition i == j becomes true at a different position each row.
j holds the descending column digit. When i != j, print that digit to fill the row.
Yes. Replace cout << "*" with any character such as # or X.
cout << j prints each digit or star on the same line. cout << "\n" ends the row after the inner loop finishes.
j runs from n down to 1 so each row prints digits in descending order with the star at position i.
After prompting, use if (!(cin >> n) || n <= 0) to reject bad input before printing.
O(n²) for n rows because each row prints n characters using a nested loop.
🤔
Did you know?
This 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.