C# Descending Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
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.
Approach
How to Solve It
One fixed-width loop idea with if/else or ternary variants.
Method
Idea
Best for
Fixed-width if/else
Space when j > i, else print j
Learning, interviews, exams
Ternary form
Same logic in one expression per cell
Shorter 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
Goal
Pattern
Grow the filled width
for (i = 1; i <= rows; i++)
Fixed column scan
for (j = rows; j >= 1; j--)
Space or digit
if (j > i) Console.Write(" "); else Console.Write(j);
Ternary form
Console.Write((j > i) ? " " : j.ToString());
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each space or digit
Console.WriteLine
Ends the current line
After the inner loop
Try it
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 result5 rows · 5 chars/row
1
21
321
4321
54321
Trace
Worked Walkthrough — rows = 4
Trace each row: leading spaces when j > i, then descending digits.
i
Spaces / digits
Printed row
1
3 spaces / 1
1
2
2 spaces / 21
21
3
1 space / 321
321
4
none / 4321
4321
Each row is always rows characters wide — spaces and digits combined.
Code
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();
}
}
}
}
Output
1
21
321
4321
54321
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();
}
}
}
}
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();
}
}
}
}
Output
1
21
321
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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².
Remember
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.
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.