Number Pattern Fill with 5 Triangle in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two Inner Loops

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:

Output
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
1

Complete C# Program

Print i..5 first, then print 5 enough times to complete the row width.

C#
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

1

Outer loop controls the start value

i goes from 5 down to 1, so each row starts smaller.

Row control
2

First inner loop prints i..5

The loop for (j = i; j <= 5; j++) prints an increasing sequence ending at 5.

Sequence
3

Second inner loop fills with 5

for (j = 1; j < i; j++) prints the constant 5 to keep every row the same width.

Fill
=

Triangle filled with 5

Every row prints 5 numbers total, so runtime is \(O(n^2)\) for width \(n\).

2

Variation — Configurable n

Let the user choose the size n, and fill with n instead of 5:

C#
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

1

Two inner loops let you build and then fill each row.

2

First loop prints i..n; second loop prints constant n.

3

Each row prints exactly n numbers.

4

Time complexity is \(O(n^2)\) for width \(n\).

❓ Frequently Asked Questions

After printing the increasing part, the second loop fills the rest of the row with 5 so every row has the same length.
Replace 5 with a variable like n (see the variation) or any constant you prefer.
O(n²) for width n because each row prints n numbers.

Explore More C# Number Patterns!

Try mixing fill-values with other sequences to build richer number grids and triangles.

All Number Patterns →
Did you know?

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.

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