Descending Numbers with Diagonal Asterisk in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Conditional Print

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:

Output
5432*
543*1
54*21
5*321
*4321
1

Complete C# Program

Loop rows with i and loop columns from 5 down to 1 using j. When i == j, print *.

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();
            }
        }
    }
}

🧠 How It Works

1

Outer loop controls the row

i runs from 1 to 5.

Rows
2

Inner loop prints 5..1

j runs from 5 down to 1 and prints the current column digit.

Columns
3

Replace the diagonal with *

When i == j, we print * instead of j.

Condition
=

Diagonal star across rows

As i increases, the star position moves left.

2

Variation — Use a Different Symbol

Replace the diagonal with # instead of *:

C#
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.WriteLine inside the inner loop

Key Takeaways

1

Descending digits come from looping j from n down to 1.

2

i == j marks a diagonal position.

3

Replacing digits with symbols is a clean use of if/else.

4

Overall time complexity is \(O(n^2)\).

❓ Frequently Asked Questions

On row 1, the star prints when j reaches 1, so the last character becomes *.
Use if (i + j == n + 1) instead of i == j.
O(n²) because there are n rows and each row prints n characters.

Explore More C# Number Patterns!

Try combining diagonals and symbols to create even more creative patterns.

All Number Patterns →
Did you know?

Diagonal conditions like i == j and i + j == n + 1 are commonly used to draw X-shapes and borders in patterns.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful