Repeating Number Triangle in C#

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:
1
22
333
4444
55555Complete C# Program
The outer loop selects the digit (i). The inner loop runs i times and prints i each time.
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
Set the height
int rows = 5; defines the number of rows.
Outer loop (choose the digit)
for (i = 1; i <= rows; i++) decides which number to print on a row.
Inner loop (repeat i times)
for (j = 1; j <= i; j++) repeats the print operation i times on that row.
New line
Console.WriteLine() moves to the next row.
Repeating triangle
Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Read the row count at runtime so the pattern can scale:
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.TryParsebefore converting - Add spaces with
Console.Write(i + " ")to make the triangle wider - Print the column index (
j) instead ofifor an incremental pattern - Use characters instead of digits for a repeating alphabet triangle
Avoid
- Forgetting
Console.WriteLine()after each row - Using
Convert.ToInt32without handling invalid input - Mixing row and column logic (outer loop should control rows)
Key Takeaways
The outer loop chooses the digit (i).
The inner loop repeats it i times.
Total prints are triangular: n(n+1)/2.
This idea extends to repeating stars, alphabets, and symbols too.
❓ Frequently Asked Questions
i = 4 so the inner loop runs 4 times and prints 4 each time.rows to 1, and keep the inner loop as 1..i to reduce repeats each row.n(n+1)/2.Explore More C# Number Patterns!
Repeating digits is a great stepping stone to repeating stars, alphabets, and other pattern variations.
The total number of printed digits in any \(n\)-row triangle like this is n(n+1)/2 — a classic triangular number formula.
12 people found this page helpful
