Shrinking Repeating Number Pattern in C#

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

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:

Output
11111
2222
333
44
5
1

Complete C# Program

The outer loop chooses the digit (i). The inner loop runs from i to rows, so it repeats fewer times each row.

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(i);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Set the size

int rows = 5; sets the maximum width.

Setup
2

Outer loop (choose digit)

for (i = 1; i <= rows; i++) decides which digit prints on each row.

Row control
3

Inner loop (repeat count shrinks)

for (j = i; j <= rows; j++) runs fewer times as i increases, shrinking the row width.

Repeat digit
4

New line

Console.WriteLine() ends each row.

Line break
=

Shrinking repeating pattern

Total prints are triangular: n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Read the row count at runtime:

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(i);
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Validate input with int.TryParse before converting
  • Add spaces with Console.Write(i + " ") to make the pattern wider
  • Reverse it to print 55555 down to 1 (see Program 11)
  • Use characters to create shrinking alphabet patterns

Avoid

  • Forgetting Console.WriteLine() after each row
  • Using Convert.ToInt32 without handling invalid input
  • Mixing up bounds (inner loop should start at i)

Key Takeaways

1

The outer loop chooses which digit to repeat.

2

The inner loop decides how many times it repeats.

3

As the inner-loop start increases, the pattern shrinks.

4

Total prints follow triangular numbers: n(n+1)/2.

❓ Frequently Asked Questions

On the first row, i = 1 and the inner loop runs from 1 to 5, so it prints 1 five times.
Use the inner loop as 1..i instead of i..rows so the repeat count grows (see Program 9).
O(n²) for n rows since total prints are n(n+1)/2.

Explore More C# Number Patterns!

Try shrinking, inverted, and alternating patterns to strengthen your loop intuition.

All Number Patterns →
Did you know?

The number of printed digits is still triangular: \(5+4+3+2+1 = 15\) for 5 rows.

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