C# Decreasing Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
In C# the outer loop runs i from rows down to 1. First print indentation spaces, then print the decreasing sequence with {0,2}.
Approach
How to Solve It
One descending outer loop with a separate indent loop and number loop.
Method
Idea
Best for
Indent + descending
Pad left; print rows..i with {0,2}
Learning, interviews, exams
User-input rows
Same logic with a variable height
Practice / 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
Goal
Pattern
Shrink the row limit
for (i = rows; i >= 1; i--)
Right-align
for (j = 1; j < i; j++) Console.Write(" ");
Descending values
for (j = rows; j >= i; j--)
Fixed-width digit
Console.Write("{0,2}", j);
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each indent or number
Console.WriteLine
Ends the current line
After both inner loops
Try it
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 result5 rows · 15 values
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Trace
Worked Walkthrough — rows = 4
Trace indentation vs the decreasing sequence. Outer i starts at 4 and shrinks.
i
Indents / numbers
Printed row
4
3 pads / 4
4
3
2 pads / 4 3
4 3
2
1 pad / 4 3 2
4 3 2
1
none / 4..1
4 3 2 1
Larger i means more indentation and fewer numbers — that is what creates the right-aligned tip at the top.
Code
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();
}
}
}
}
Output
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
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();
}
}
}
}
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();
}
}
}
}
Output
3
3 2
3 2 1
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.