Number Pattern Fill with 5 Triangle in C#

What You’ll Learn
How to print a number pattern in C# where each row begins with an increasing sequence up to 5, and the remaining positions are filled with 5:
5 5 5 5 5, then 4 5 5 5 5, ... until 1 2 3 4 5.
⭐ Pattern Output
For n = 5, the pattern looks like this:
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5Complete C# Program
Print i..5 first, then print 5 enough times to complete the row width.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 5; i >= 1; i--)
{
for (j = i; j <= 5; j++)
Console.Write(j + " ");
for (j = 1; j < i; j++)
Console.Write(5 + " ");
Console.WriteLine();
}
}
}
}🧠 How It Works
Outer loop controls the start value
i goes from 5 down to 1, so each row starts smaller.
First inner loop prints i..5
The loop for (j = i; j <= 5; j++) prints an increasing sequence ending at 5.
Second inner loop fills with 5
for (j = 1; j < i; j++) prints the constant 5 to keep every row the same width.
Triangle filled with 5
Every row prints 5 numbers total, so runtime is \(O(n^2)\) for width \(n\).
Variation — Configurable n
Let the user choose the size n, and fill with n instead of 5:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter n: ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = n; i >= 1; i--)
{
for (int j = i; j <= n; j++)
Console.Write(j + " ");
for (int j = 1; j < i; j++)
Console.Write(n + " ");
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Replace the fill value (
5) with any constant to create new patterns - Remove spaces for a compact version (use
Console.Write(j)) - Print a right-shifted version by adding leading spaces
Avoid
- Forgetting the second loop (rows will have different widths)
- Hard-coding 5 if you want a reusable function (prefer a variable)
Key Takeaways
Two inner loops let you build and then fill each row.
First loop prints i..n; second loop prints constant n.
Each row prints exactly n numbers.
Time complexity is \(O(n^2)\) for width \(n\).
❓ Frequently Asked Questions
5 with a variable like n (see the variation) or any constant you prefer.Explore More C# Number Patterns!
Try mixing fill-values with other sequences to build richer number grids and triangles.
Patterns that keep a constant row width often use a “build + fill” approach: first generate a sequence, then pad the rest with a constant value.
12 people found this page helpful
