C# Descending Number Pattern (Diagonal Asterisk)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

One nested-loop idea with two useful variants — star fill and custom fill.

MethodIdeaBest for
Diagonal swapDescend j = n..1; print * when i == jLearning, interviews, exams
Custom fillSame loops; replace "*" with any characterPractice 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

GoalPattern
Walk each rowfor (i = 1; i <= n; i++)
Descend digitsfor (j = n; j >= 1; j--)
Place the starif (i == j) Console.Write("*");
Print the digitelse Console.Write(j);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit j or the *
Console.WriteLineEnds the current lineAfter the inner loop

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 result n = 5 · 25 chars
5432*
543*1
54*21
5*321
*4321

Worked Walkthrough — n = 4

Trace each row index i and where i == j places the star among digits 4..1.

iStar when j =Printed row
11 (rightmost)432*
2243*1
334*21
44 (leftmost)*321

Each row prints exactly n characters, so total work is n².

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

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

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

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.

Time and Space Complexity

ProgramTimeExtra 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².

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.

Next: Palindrome Number Triangle

Print ascending then descending digits on each row for a palindrome look.

Program 27 tutorial →

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