Left-Shifted Number Triangle in C#

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
12345
2345
345
45
5
1

Complete C# Program

The outer loop controls the starting number. The inner loop prints from i to rows.

C#
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

1

Pick the height

int rows = 5; sets how many rows (and the max number) to print.

Setup
2

Outer loop (row start)

for (i = 1; i <= rows; i++) selects the first number to print on each row.

Row control
3

Inner loop (print i..rows)

for (j = i; j <= rows; j++) prints a decreasing-length sequence because i increases each row.

Number printing
4

New line

Console.WriteLine() moves to the next row.

Line break
=

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.

2

Variation — User Input Version

Read the number of rows at runtime using Console.ReadLine():

C#
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.TryParse before converting
  • Print spaces between numbers for readability
  • Reverse the inner loop to print rows..i instead
  • Try right-aligning by printing leading spaces each row

Avoid

  • Putting Console.WriteLine() inside the inner loop (breaks the row)
  • Using Convert.ToInt32 without handling invalid input
  • Mixing up loop bounds (the inner loop must start at i)

Key Takeaways

1

The outer loop decides the row start value (i).

2

The inner loop prints from i to rows, so rows shrink as i increases.

3

Total prints are triangular: n(n+1)/2O(n²).

4

This pattern is a stepping stone to more complex right-aligned and inverted shapes.

❓ Frequently Asked Questions

Because each next row starts at a higher number (i increases), so numbers below i are not printed anymore.
Yes. Start the outer loop at i = 0 and adjust the end value accordingly. Most pattern examples use 1-based counting for readability.
O(n²) for 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.

All Number Patterns →
Did you know?

This pattern prints a total of n(n+1)/2 numbers. That triangular count appears in many loop problems and even in combinatorics.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful