Descending Numbers with Diagonal Asterisk in C#

What You’ll Learn
How to print descending digits from 5 to 1, but replace one diagonal position with * on each row.
⭐ Pattern Output
For n = 5, the pattern looks like this:
5432*
543*1
54*21
5*321
*4321Complete C# Program
Loop rows with i and loop columns from 5 down to 1 using j. When i == j, print *.
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();
}
}
}
}🧠 How It Works
Outer loop controls the row
i runs from 1 to 5.
Inner loop prints 5..1
j runs from 5 down to 1 and prints the current column digit.
Replace the diagonal with *
When i == j, we print * instead of j.
Diagonal star across rows
As i increases, the star position moves left.
Variation — Use a Different Symbol
Replace the diagonal with # instead of *:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int n = 5;
for (int i = 1; i <= n; i++)
{
for (int j = n; j >= 1; j--)
{
if (i == j) Console.Write("#");
else Console.Write(j);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Make the size dynamic with a variable
n - Print spaces between digits to widen the pattern
- Create the opposite diagonal with
i + j == n + 1
Avoid
- Changing the inner loop order unless you also adjust the condition logic
- Using
Console.WriteLineinside the inner loop
Key Takeaways
Descending digits come from looping j from n down to 1.
i == j marks a diagonal position.
Replacing digits with symbols is a clean use of if/else.
Overall time complexity is \(O(n^2)\).
❓ Frequently Asked Questions
j reaches 1, so the last character becomes *.if (i + j == n + 1) instead of i == j.Explore More C# Number Patterns!
Try combining diagonals and symbols to create even more creative patterns.
Diagonal conditions like i == j and i + j == n + 1 are commonly used to draw X-shapes and borders in patterns.
12 people found this page helpful
