C# Decreasing Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned decreasing triangle indents each row, then prints values from rows down to the current i — so every row starts at the same high number.

Remember
Rule: indent (i-1) times, then print rows..i in width 2

         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1   ← rows = 5

In C# the outer loop runs i from rows down to 1. First print indentation spaces, then print the decreasing sequence with {0,2}.

How to Solve It

One descending outer loop with a separate indent loop and number loop.

MethodIdeaBest for
Indent + descendingPad left; print rows..i with {0,2}Learning, interviews, exams
User-input rowsSame logic with a variable heightPractice / demos

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i-1:
        print two spaces
    for j from rows down to i:
        print j (width 2)
    print newline

Cheat sheet

GoalPattern
Shrink the row limitfor (i = rows; i >= 1; i--)
Right-alignfor (j = 1; j < i; j++) Console.Write(" ");
Descending valuesfor (j = rows; j >= i; j--)
Fixed-width digitConsole.Write("{0,2}", j);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach indent or number
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the row count and the right-aligned decreasing triangle updates instantly.

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 · 15 values
         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1

Worked Walkthrough — rows = 4

Trace indentation vs the decreasing sequence. Outer i starts at 4 and shrinks.

iIndents / numbersPrinted row
43 pads / 44
32 pads / 4 34 3
21 pad / 4 3 24 3 2
1none / 4..14 3 2 1

Larger i means more indentation and fewer numbers — that is what creates the right-aligned tip at the top.

C# Programs

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

Example 1 — Fixed rows = 5

Descending outer loop: indent, then print rows..i with width-2 formatting.

C#
using System;

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

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

                for (j = rows; j >= i; j--)
                    Console.Write("{0,2}", j);

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i starts at 5 and shrinks to 1 — more numbers appear each row.

2. Indent. Print " " while j < i so shorter rows stay right-aligned.

3. Numbers. Print j from rows down to i with {0,2} for fixed columns.

Example 2 — User Input

Read the row count; both the indent and number loops use rows.

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 = rows; i >= 1; i--)
            {
                for (j = 1; j < i; j++)
                    Console.Write("  ");

                for (j = rows; j >= i; j--)
                    Console.Write("{0,2}", j);

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same alignment. Three rows start each line at 3 and grow down to 3 2 1.

Example 3 — Compact rows = 3

A smaller fixed demo — same indent-and-descend 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 = rows; i >= 1; i--)
            {
                for (j = 1; j < i; j++)
                    Console.Write("  ");

                for (j = rows; j >= i; j--)
                    Console.Write("{0,2}", j);

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Indent i - 1 times; print rows down to i.

2. Full width. The last row has no indentation — just 3 2 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

skip indent

Omit the indentation loop

Without for (j = 1; j < i; j++), every row starts at the left margin.

ascending i

Loop i from 1 up instead of down

The tip and base swap order. Keep for (i = rows; i >= 1; i--).

no format

Use Write(j) without width

Columns drift for multi-digit values. Prefer Console.Write("{0,2}", j).

rows = 1

Single 1

Output is just the formatted 1 — no indentation.

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
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Indent steps plus printed numbers across all rows sum to roughly n² work.

Key Takeaways

  • Rule: indent i - 1 times, then print rows..i with width 2.
  • Outer direction: i runs from rows down to 1 so the tip appears first.
  • Write vs WriteLine: indents and numbers stay on the line; WriteLine advances after both loops.
  • Next step: Program 37 prints a palindrome number triangle (1, 212, 32123…).

One line: for each i from rows down to 1, indent then print the decreasing sequence rows..i.

Frequently Asked Questions

A right-aligned decreasing triangle: row 1 prints 5, row 2 prints 5 4, row 3 prints 5 4 3, and so on until 5 4 3 2 1.
An indentation loop prints two spaces while j < i before the number loop runs, pushing shorter rows to the right.
The format specifier reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for (j = rows; j >= i; j--), so every row begins at rows and counts down to the current i.
Program 35 uses a continuous counter k. Program 36 restarts from rows on each row with a separate indentation loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total printed numbers are 1 + 2 + ... + n = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single number with no leading indentation.

Did you know?

Each row starts from rows and counts down to i. An indentation loop prints two spaces per step (j = 1..i-1), then {0,2} keeps number columns aligned.

Next: Palindrome Number Triangle

Build rows that read the same forward and backward (1, 212, 32123…).

Program 37 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