Bidirectional Number Triangle in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
If/Else Mapping

What You’ll Learn

How to print a shrinking repeating number pattern in C#:

11111, 2222, 333, 22, 1.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
11111
2222
333
22
1
1

Complete C# Program

The inner loop controls the shrinking length, while an if chooses which digit to repeat.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;

            for (i = 1; i <= 5; i++)
            {
                for (j = i; j <= 5; j++)
                {
                    if (i < 4)
                        Console.Write(i);
                    else
                        Console.Write(6 - i);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Outer loop controls the row number

i goes from 1 to 5.

Rows
2

Inner loop shrinks the row length

The loop j = i to 5 prints fewer times as i increases.

Shrink
3

Pick the digit to repeat

For i < 4 we print i. For i = 4 and 5 we print 6 - i, producing 2 then 1.

If/Else
=

Shrinking repeating pattern

The last two rows mirror down to 22 and 1.

2

Variation — Custom Rows

Generalize the same logic for any number of rows:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter rows: ");
            int rows = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= rows; i++)
            {
                int val = (i < rows - 1) ? i : (rows + 1 - i);

                for (int j = i; j <= rows; j++)
                {
                    Console.Write(val);
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Add a space between digits for readability
  • Try a symmetric version by mirroring values for all rows
  • Convert the logic into a reusable function

Avoid

  • Resetting the value inside the inner loop
  • Mixing up row-length logic (keep shrinking tied to i)

Key Takeaways

1

The inner loop j=i..rows shrinks the row length.

2

A simple mapping prints 2 then 1 on the last rows.

3

This is a good exercise for nested loops + conditions.

4

Total prints are triangular, so complexity is \(O(n^2)\).

❓ Frequently Asked Questions

Because the code uses 6 - i when i is 4 or 5, producing 2 and 1.
Yes. Print a space only between digits, or build the row into a string then trim it before printing.
O(n²) for n rows since total prints are about n(n+1)/2.

Explore More C# Number Patterns!

Repeating-number patterns are perfect practice for loop boundaries and conditional logic.

All Number Patterns →
Did you know?

A small conditional mapping like 6 - i can “mirror” values and create interesting patterns with minimal code.

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