Repeating Number Triangle in C#

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

What You’ll Learn

How to print a repeating number triangle in C#. Each row prints the same digit repeatedly: 1, 22, 333, 4444, and 55555.

This is a simple pattern for understanding that the inner loop controls repetition, while the outer loop chooses which digit gets repeated.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
22
333
4444
55555
1

Complete C# Program

The outer loop selects the digit (i). The inner loop runs i times and prints i each time.

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 = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Set the height

int rows = 5; defines the number of rows.

Setup
2

Outer loop (choose the digit)

for (i = 1; i <= rows; i++) decides which number to print on a row.

Row control
3

Inner loop (repeat i times)

for (j = 1; j <= i; j++) repeats the print operation i times on that row.

Repeat digit
4

New line

Console.WriteLine() moves to the next row.

Line break
=

Repeating triangle

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

2

Variation — User Input Version

Read the row count at runtime so the pattern can scale:

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 = 1; j <= i; 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 triangle wider
  • Print the column index (j) instead of i for an incremental pattern
  • Use characters instead of digits for a repeating alphabet triangle

Avoid

  • Forgetting Console.WriteLine() after each row
  • Using Convert.ToInt32 without handling invalid input
  • Mixing row and column logic (outer loop should control rows)

Key Takeaways

1

The outer loop chooses the digit (i).

2

The inner loop repeats it i times.

3

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

4

This idea extends to repeating stars, alphabets, and symbols too.

❓ Frequently Asked Questions

On that row, i = 4 so the inner loop runs 4 times and prints 4 each time.
Yes. Make the outer loop count down from rows to 1, and keep the inner loop as 1..i to reduce repeats each row.
O(n²) for n rows since total prints are n(n+1)/2.

Explore More C# Number Patterns!

Repeating digits is a great stepping stone to repeating stars, alphabets, and other pattern variations.

All Number Patterns →
Did you know?

The total number of printed digits in any \(n\)-row triangle like this is n(n+1)/2 — a classic triangular number formula.

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