Shrinking Repeating Number Pattern in C#

What You’ll Learn
How to print a shrinking repeating number pattern in C#. Each row repeats the row digit, but the row becomes shorter each time: 11111, 2222, 333, 44, 5.
This pattern is useful for practicing how a changing inner-loop start value controls row width.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
11111
2222
333
44
5Complete C# Program
The outer loop chooses the digit (i). The inner loop runs from i to rows, so it repeats fewer times each row.
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(i);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Set the size
int rows = 5; sets the maximum width.
Outer loop (choose digit)
for (i = 1; i <= rows; i++) decides which digit prints on each row.
Inner loop (repeat count shrinks)
for (j = i; j <= rows; j++) runs fewer times as i increases, shrinking the row width.
New line
Console.WriteLine() ends each row.
Shrinking repeating pattern
Total prints are triangular: n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Read the row count at runtime:
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(i);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Validate input with
int.TryParsebefore converting - Add spaces with
Console.Write(i + " ")to make the pattern wider - Reverse it to print
55555down to1(see Program 11) - Use characters to create shrinking alphabet patterns
Avoid
- Forgetting
Console.WriteLine()after each row - Using
Convert.ToInt32without handling invalid input - Mixing up bounds (inner loop should start at
i)
Key Takeaways
The outer loop chooses which digit to repeat.
The inner loop decides how many times it repeats.
As the inner-loop start increases, the pattern shrinks.
Total prints follow triangular numbers: n(n+1)/2.
❓ Frequently Asked Questions
i = 1 and the inner loop runs from 1 to 5, so it prints 1 five times.1..i instead of i..rows so the repeat count grows (see Program 9).n(n+1)/2.Explore More C# Number Patterns!
Try shrinking, inverted, and alternating patterns to strengthen your loop intuition.
The number of printed digits is still triangular: \(5+4+3+2+1 = 15\) for 5 rows.
12 people found this page helpful
