A descending number pattern with a diagonal asterisk prints n..1 on every row, but replaces the digit where i == j with * — so the star slides left each line.
Remember
Rule: print j = n..1; if i == j print *
5432*
543*1
54*21
5*321
*4321 ← n = 5
In C# the outer loop is the row index i, the inner loop counts j from n down to 1, and an if (i == j) decides between * and the digit j. Then Console.WriteLine() ends each row.
Approach
How to Solve It
One nested-loop idea with two useful variants — star fill and custom fill.
Method
Idea
Best for
Diagonal swap
Descend j = n..1; print * when i == j
Learning, interviews, exams
Custom fill
Same loops; replace "*" with any character
Practice variants
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 each row
for (i = 1; i <= n; i++)
Descend digits
for (j = n; j >= 1; j--)
Place the star
if (i == j) Console.Write("*");
Print the digit
else Console.Write(j);
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit j or the *
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the size and the diagonal-star pattern updates instantly — including the total character count.
Whole numbers from 1 to 9 (keeps digits single-width). 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 = 4
Trace each row index i and where i == j places the star among digits 4..1.
i
Star when j =
Printed row
1
1 (rightmost)
432*
2
2
43*1
3
3
4*21
4
4 (leftmost)
*321
Each row prints exactly n characters, so total work is n².
Code
C# Programs
Three complete programs: fixed size, user-input size, and a custom diagonal fill. Use View Output for sample results.
Example 1 — Fixed n = 5
Descend j from 5 to 1; swap in * when i == j.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (i == j)
Console.Write("*");
else
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output
5432*
543*1
54*21
5*321
*4321
How It Works
1. Row index.i runs 1..5 and marks which descending digit becomes a star.
2. Descend columns.j prints 5, 4, 3, 2, 1 unless i == j.
3. Newline.WriteLine() after the inner loop starts the next row.
Example 2 — User Input
Read n at runtime. Prefer int.TryParse so bad input does not throw.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int n;
int i, j;
Console.Write("Enter size: ");
if (!int.TryParse(Console.ReadLine(), out n) || n < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = 1; i <= n; i++)
{
for (j = n; j >= 1; j--)
{
if (i == j)
Console.Write("*");
else
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output (when user enters 4)
432*
43*1
4*21
*321
How It Works
1. Validate n.TryParse rejects non-numeric input; require n >= 1.
2. Same nested loops. For n = 4 the star moves from the right end to the left end across four rows.
Example 3 — Custom Fill #
Same descending rows — only the diagonal character changes.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int n = 5;
int i, j;
for (i = 1; i <= n; i++)
{
for (j = n; j >= 1; j--)
{
if (i == j)
Console.Write("#");
else
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output
5432#
543#1
54#21
5#321
#4321
How It Works
1. Swap the fill. Replace "*" with "#" (or any single character).
2. Same structure. The descending digit loop and i == j test stay unchanged.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong else
Write(i) instead of Write(j)
That repeats the row number. Always print the column digit j in the else branch.
ascend j
j = 1..n instead of n..1
That prints ascending digits. Keep for (j = n; j >= 1; j--).
WriteLine early
WriteLine inside the inner loop
That puts each character on its own line. Call WriteLine() only after the row finishes.
n = 1
Single *
Output is just * — i and j are both 1.
n ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Star fill (Examples 1–2)
O(n²)
O(1)
Custom fill (Example 3)
O(n²)
O(1)
There are n rows printing n characters each, so total work is n².
Remember
Key Takeaways
Rule: print j = n..1; when i == j, print * instead.
Else prints j: the column digit fills every non-star position.
Write vs WriteLine: digits and stars stay on the line; WriteLine advances after each row.
Next step: Program 27 builds a palindrome number triangle.
One line: for each row, print n..1 and replace the digit where i == j with *.
Frequently Asked Questions
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 (5, 4, 3, 2, 1). When i != j, print that digit to fill the row.
Yes. Replace Console.Write("*") with any character or string — see Example 3 with #.
Console.Write prints each digit or star on the same line. Console.WriteLine 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.
Replace 5 with n in both loops — see Example 2 and the live preview.
O(n²) for n rows because each row prints n characters using a nested loop.
Prefer int.TryParse(Console.ReadLine(), out n) so bad input does not throw FormatException.
Only one row prints — a single *.
🤔
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.