Inverted Repeating Number Triangle in C#

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:
55555
4444
333
22
1Complete C# Program
The outer loop picks the digit from rows down to 1. The inner loop repeats that digit i times.
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
Set the height
int rows = 5; defines the starting digit and the number of rows.
Outer loop (digit decreases)
for (i = rows; i >= 1; i--) chooses which digit to print (5, 4, 3, 2, 1).
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.
New line
Console.WriteLine() ends each row.
Inverted repeating triangle
Total prints are still 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 = 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.TryParsebefore converting - Add spaces with
Console.Write(i + " ")for readability - Print an ascending repeating triangle by counting
iupward (see Program 9) - Use characters to build an inverted alphabet pattern
Avoid
- Forgetting
Console.WriteLine()after each row - Using
Convert.ToInt32without handling invalid input - Changing the inner loop bounds (it controls the row width)
Key Takeaways
The outer loop selects the digit from rows down to 1.
The inner loop repeats the digit i times.
Total prints are triangular: n(n+1)/2.
This is an easy inversion of a repeating triangle.
❓ Frequently Asked Questions
i = rows (5) on the first iteration, and the inner loop runs 5 times, printing 5 each time.rows down to i and print i inside it (see Program 10).n(n+1)/2.Explore More C# Number Patterns!
Inversions are a simple way to generate new patterns from the same nested-loop idea.
This pattern prints \(n(n+1)/2\) digits in total for \(n\) rows—another triangular-number classic.
12 people found this page helpful
