Right-Aligned Incremental Number Triangle in C#

What You’ll Learn
How to print a right-aligned triangle where numbers increase continuously: 1, then 2 3, then 4 5 6, and so on.
You’ll practice two key ideas: nested loops (row/column control) and formatted output (fixed-width alignment using {0,3}).
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15Complete C# Program
We print spaces until the diagonal, then print the next number k with fixed-width formatting so columns stay aligned.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
int k = 1;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,3}", k++);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Initialize rows and counter
rows controls the height, and k starts at 1 and increases every time we print a number.
Outer loop (rows)
for (i = 1; i <= rows; i++) prints one line per row.
Inner loop (alignment + numbers)
The loop for (j = rows; j >= 1; j--) prints indentation while j > i, then prints numbers when j <= i.
Fixed-width printing
Console.Write("{0,3}", k++) reserves 3 columns per number so 10, 11, etc. don’t shift the triangle.
Right-aligned incremental triangle
Total numbers printed are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Rows
Read the row count at runtime. The pattern logic stays the same.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
int k = 1;
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 = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,3}", k++);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Increase the width from
{0,3}to{0,4}for larger row counts - Print left-aligned by removing the indentation branch
- Reset
keach row to make1, then1 2, then1 2 3 - Replace numbers with characters to create alphabet triangles
Avoid
- Using inconsistent spacing (it will misalign once numbers reach two digits)
- Not validating input when using user-entered rows
- Forgetting
Console.WriteLine()at the end of each row
Key Takeaways
A counter k creates a continuous sequence across rows.
Right alignment comes from printing blanks while j > i.
{0,3} keeps columns aligned when values become two digits.
Work grows as O(n²) because total prints are n(n+1)/2.
❓ Frequently Asked Questions
k is declared outside the loops and is incremented each time a number is printed, so it continues across rows." ". Then print numbers directly with an inner loop from 1 to i.j with the current row i.Explore More C# Number Patterns!
Practice nested loops with triangles, pyramids, and incremental patterns.
Using {0,3} (or any fixed width) is a simple trick to keep console patterns aligned as values grow from single digits to double digits and beyond.
12 people found this page helpful
