C# Descending Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned descending number triangle pads each row with leading spaces, then prints digits from i down to 1 so the triangle lines up on the right.

Remember
Rule: (rows - i) spaces + digits i..1

    1
   21
  321
 4321
54321   ← 5 rows

In C# the inner loop always runs from rows down to 1. When j > i print a space; otherwise print j. That keeps every row the same width.

How to Solve It

One fixed-width loop idea with if/else or ternary variants.

MethodIdeaBest for
Fixed-width if/elseSpace when j > i, else print jLearning, interviews, exams
Ternary formSame logic in one expression per cellShorter demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to 1:
        if j > i: print space else print j
    print newline

Cheat sheet

GoalPattern
Grow the filled widthfor (i = 1; i <= rows; i++)
Fixed column scanfor (j = rows; j >= 1; j--)
Space or digitif (j > i) Console.Write(" "); else Console.Write(j);
Ternary formConsole.Write((j > i) ? " " : j.ToString());
End the rowConsole.WriteLine();

Write vs WriteLine

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

Live Preview

Change the row count and the right-aligned triangle updates instantly — including the character count per row.

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 5 rows · 5 chars/row
    1
   21
  321
 4321
54321

Worked Walkthrough — rows = 4

Trace each row: leading spaces when j > i, then descending digits.

iSpaces / digitsPrinted row
13 spaces / 11
22 spaces / 2121
31 space / 321321
4none / 43214321

Each row is always rows characters wide — spaces and digits combined.

C# Programs

Three complete programs: fixed height, user input with a ternary, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

One fixed-width descending loop: print a space when j > i, otherwise print 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 (j > i)
                        Console.Write(" ");
                    else
                        Console.Write(j);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i grows from 1 to 5 — each pass adds one more digit on the right.

2. Inner loop. j scans from 5 down to 1. When j > i print a space; otherwise print j.

3. Newline. WriteLine() after the inner loop starts the next wider fill.

Example 2 — User Input

Read the row count and use a ternary for compact space-or-digit logic.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j;

            Console.Write("Enter rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            for (i = 1; i <= rows; i++)
            {
                for (j = rows; j >= 1; j--)
                    Console.Write((j > i) ? " " : j.ToString());

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

2. Same alignment. The ternary picks space vs digit; four rows end at 4321.

Example 3 — Compact rows = 3

A smaller if/else demo — same fixed-width idea, easier to trace by hand.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 3;
            int i, j;

            for (i = 1; i <= rows; i++)
            {
                for (j = rows; j >= 1; j--)
                {
                    if (j > i) Console.Write(" ");
                    else Console.Write(j);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Spaces while j > i; digits i..1 when j <= i.

2. Full width. The last row is 321 with no leading spaces.

Edge Cases & Pitfalls

Check these before calling the solution done.

skip spaces

Omit the space branch

Without spaces the triangle becomes left-aligned (like Program 3). Keep Write(" ") when j > i.

wrong direction

Loop j ascending instead of descending

for (j = 1; j <= rows; j++) prints ascending digits. Keep j from rows down to 1.

WriteLine early

WriteLine inside the inner loop

That puts each character on its own line. Call WriteLine() only after the inner loop finishes.

rows = 1

Single 1

Output is just 1 — no leading spaces.

rows ≤ 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
If/else loops (Examples 1, 3)O(n²)O(1)
Ternary form (Example 2)O(n²)O(1)

There are n rows; each runs one inner loop of width n, so total work is n².

Key Takeaways

  • Rule: space when j > i; otherwise print descending j (i..1).
  • Fixed width: the inner loop always runs rows times so columns stay right-aligned.
  • Write vs WriteLine: spaces and digits stay on the line; WriteLine advances after the row.
  • Next step: Program 31 prints a number-star diamond pattern.

One line: for each row, scan j from rows to 1 and print a space or digit to build a right-aligned i..1 triangle.

Frequently Asked Questions

Leading spaces are printed while j > i. Smaller rows get more spaces, pushing digits to the right edge.
The inner loop runs j from rows down to 1. When j <= i, it prints j — naturally producing i..1 on each row.
Running j from rows down to 1 on every row keeps column alignment. Spaces fill positions where j > i.
Program 3 prints a left-aligned reverse descending triangle with no leading spaces. Program 30 pads with spaces for right alignment.
Replace 5 with rows in the inner loop bound — see Example 2.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
One row prints with no leading spaces — just 1.
Yes — Console.Write((j > i) ? " " : j.ToString()) compacts the if/else logic.

Did you know?

This pattern uses a fixed column width (rows). For each row i, the inner loop prints spaces while j > i, then prints digits in descending order — producing a right-aligned triangle.

Next: Number-Star Diamond

Combine numbers and asterisks into a diamond-shaped pattern.

Program 31 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful