Inverted Repeating Number Triangle in C#

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

What You’ll Learn

How to print an inverted repeating number triangle in C#. Each row repeats the same digit, but the row length shrinks as the digit decreases: 55555, 4444, 333, 22, 1.

This pattern is a clean way to practice an outer loop that counts downward and an inner loop that repeats a value.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
55555
4444
333
22
1
1

Complete C# Program

The outer loop picks the digit from rows down to 1. The inner loop repeats that digit i times.

C#
using System;

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

            for (i = rows; i >= 1; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Set the height

int rows = 5; defines the starting digit and the number of rows.

Setup
2

Outer loop (digit decreases)

for (i = rows; i >= 1; i--) chooses which digit to print (5, 4, 3, 2, 1).

Row control
3

Inner loop (repeat i times)

for (j = 1; j <= i; j++) repeats the digit so row 5 prints 5 times, row 4 prints 4 times, and so on.

Repeat digit
4

New line

Console.WriteLine() ends each row.

Line break
=

Inverted repeating triangle

Total prints are still 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 = rows; i >= 1; 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 + " ") for readability
  • Print an ascending repeating triangle by counting i upward (see Program 9)
  • Use characters to build an inverted alphabet pattern

Avoid

  • Forgetting Console.WriteLine() after each row
  • Using Convert.ToInt32 without handling invalid input
  • Changing the inner loop bounds (it controls the row width)

Key Takeaways

1

The outer loop selects the digit from rows down to 1.

2

The inner loop repeats the digit i times.

3

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

4

This is an easy inversion of a repeating triangle.

❓ Frequently Asked Questions

Because i = rows (5) on the first iteration, and the inner loop runs 5 times, printing 5 each time.
Use an inner loop that counts from rows down to i and print i inside it (see Program 10).
O(n²) for n rows since total prints are n(n+1)/2.

Explore More C# Number Patterns!

Inversions are a simple way to generate new patterns from the same nested-loop idea.

All Number Patterns →
Did you know?

This pattern prints \(n(n+1)/2\) digits in total for \(n\) rows—another triangular-number classic.

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