Right-Aligned Decreasing Number Triangle in C#

What You’ll Learn
How to print a right-aligned triangle where each row shows a decreasing sequence starting from rows (5 in this example).
You’ll use one loop for indentation (spaces) and another loop to print the numbers rows down to the current row limit.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1Complete C# Program
The first inner loop prints indentation spaces. The second inner loop prints the decreasing numbers from rows down to i.
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
Set the number of rows
int rows = 5; controls both the height and the starting number for each row.
Outer loop (top to bottom)
for (i = rows; i >= 1; i--) decides how many numbers are printed on each row.
Indentation loop
for (j = 1; j < i; j++) prints two spaces per step, so higher rows shift to the right.
Number loop (decreasing sequence)
for (j = rows; j >= i; j--) prints rows, rows-1, ..., down to i for the current row.
Right-aligned decreasing triangle
Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Rows
Read the size at runtime. This version starts each row from rows automatically.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
Console.Write("Enter number of rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
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();
}
}
}
}💡 Tips for Enhancement
Try These
- Change the indentation from
" "to" "for wider spacing - Increase the width from
{0,2}to{0,3}for larger row counts - Start from a custom maximum (e.g., 9) by using
maxinstead ofrows - Print an ascending sequence by looping
jupward
Avoid
- Mixing different indent widths across rows (it breaks alignment)
- Forgetting
Console.WriteLine()after each row - Using a very small width when numbers become two digits
Key Takeaways
Two inner loops make the pattern easy: one for spaces, one for numbers.
The decreasing sequence comes from for (j = rows; j >= i; j--).
{0,2} keeps console output aligned (use a bigger width for larger values).
Total prints are triangular numbers: n(n+1)/2 \(\Rightarrow\) O(n²).
❓ Frequently Asked Questions
rows (5 here) and counts down to i.rows with a separate max variable and use it as the starting value in the number loop.{0,3} or {0,4}) so multi-digit values stay aligned.Explore More C# Number Patterns!
Keep practicing with aligned triangles and sequence-based patterns.
Console alignment becomes much easier when you print each number with a fixed width (like {0,2}). It keeps columns stable as the shape grows.
12 people found this page helpful
