Left-Shifted Number Triangle in C#

What You’ll Learn
How to print a left-shifted number triangle in C# using nested for loops. Each row starts at the row number and prints up to rows, so every next line becomes shorter: 12345, 2345, 345, 45, 5.
This is a good pattern for learning how changing the inner loop start value affects the output shape.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
12345
2345
345
45
5Complete C# Program
The outer loop controls the starting number. The inner loop prints from i to rows.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Pick the height
int rows = 5; sets how many rows (and the max number) to print.
Outer loop (row start)
for (i = 1; i <= rows; i++) selects the first number to print on each row.
Inner loop (print i..rows)
for (j = i; j <= rows; j++) prints a decreasing-length sequence because i increases each row.
New line
Console.WriteLine() moves to the next row.
Left-shifted number triangle
Total printed digits: 5+4+3+2+1 = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Read the number of rows at runtime using Console.ReadLine():
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = i; j <= rows; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Validate input with
int.TryParsebefore converting - Print spaces between numbers for readability
- Reverse the inner loop to print
rows..iinstead - Try right-aligning by printing leading spaces each row
Avoid
- Putting
Console.WriteLine()inside the inner loop (breaks the row) - Using
Convert.ToInt32without handling invalid input - Mixing up loop bounds (the inner loop must start at
i)
Key Takeaways
The outer loop decides the row start value (i).
The inner loop prints from i to rows, so rows shrink as i increases.
Total prints are triangular: n(n+1)/2 → O(n²).
This pattern is a stepping stone to more complex right-aligned and inverted shapes.
❓ Frequently Asked Questions
i increases), so numbers below i are not printed anymore.i = 0 and adjust the end value accordingly. Most pattern examples use 1-based counting for readability.n rows because you print n(n+1)/2 numbers in total.Explore More C# Number Patterns!
Keep practicing nested loops with pyramids, inversions, and alternating row patterns.
This pattern prints a total of n(n+1)/2 numbers. That triangular count appears in many loop problems and even in combinatorics.
12 people found this page helpful
