Bidirectional Number Triangle in C#

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:
11111
2222
333
22
1Complete C# Program
The inner loop controls the shrinking length, while an if chooses which digit to repeat.
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
Outer loop controls the row number
i goes from 1 to 5.
Inner loop shrinks the row length
The loop j = i to 5 prints fewer times as i increases.
Pick the digit to repeat
For i < 4 we print i. For i = 4 and 5 we print 6 - i, producing 2 then 1.
Shrinking repeating pattern
The last two rows mirror down to 22 and 1.
Variation — Custom Rows
Generalize the same logic for any number of rows:
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
The inner loop j=i..rows shrinks the row length.
A simple mapping prints 2 then 1 on the last rows.
This is a good exercise for nested loops + conditions.
Total prints are triangular, so complexity is \(O(n^2)\).
❓ Frequently Asked Questions
6 - i when i is 4 or 5, producing 2 and 1.n(n+1)/2.Explore More C# Number Patterns!
Repeating-number patterns are perfect practice for loop boundaries and conditional logic.
A small conditional mapping like 6 - i can “mirror” values and create interesting patterns with minimal code.
12 people found this page helpful
