Centered Continuous Number Pyramid in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spacing + Counter

What You’ll Learn

How to print a centered number pyramid in C# where numbers increase continuously across rows.

For this example, the output is: 1, then 2 3 4, then 5 6 7 8 9.

⭐ Pattern Output

The pattern looks like this:

Output
  1 
  2 3 4
5 6 7 8 9
1

Complete C# Program

We loop over odd row widths (1, 3, 5). For each row we scan columns from 5 down to 1 and print spaces until the row boundary is reached, then print k++.

C#
using System;

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

            for (i = 1; i <= 5; i += 2)
            {
                for (j = 5; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write(" ");
                    else
                        Console.Write(k++ + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Initialize the counter

k = 1 stores the next number to print.

Counter
2

Outer loop chooses odd row width

i takes 1, 3, 5 so the pyramid grows by 2 numbers per row.

Row width
3

Print spaces then numbers

When j > i we print a space; otherwise we print k++ followed by a space.

Alignment
=

Centered pyramid

Numbers increase continuously because k never resets.

2

Variation — Custom Max Width

Let the user choose the maximum odd width (like 7 or 9) and generate the pyramid accordingly:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the maximum odd width: ");
            int max = Convert.ToInt32(Console.ReadLine());

            if (max % 2 == 0) max -= 1;
            if (max < 1) return;

            int k = 1;
            for (int i = 1; i <= max; i += 2)
            {
                for (int j = max; j >= 1; j--)
                {
                    if (j > i) Console.Write(" ");
                    else Console.Write(k++ + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Print the pyramid with fixed-width formatting (pad numbers) once values become 2+ digits
  • Reset k each row to restart numbering per row
  • Replace numbers with letters to create alphabet pyramids

Avoid

  • Using even widths (this version assumes odd row sizes)
  • Forgetting spaces (alignment will break)

Key Takeaways

1

Use a counter k to continue numbering across rows.

2

Odd row widths (1,3,5,...) create a pyramid shape.

3

Leading spaces align the numbers to the center.

4

The same idea works for other centered patterns.

❓ Frequently Asked Questions

Because the inner loop prints spaces while j > i. For i = 1 and max width 5, three positions are spaces, so the single number appears centered.
Yes. Move k = 1 inside the outer loop so it resets for every row.
O(n²) with max width n, because you loop across n columns for about n/2 rows.

Explore More C# Number Patterns!

Centered patterns are great practice for combining loops with alignment logic.

All Number Patterns →
Did you know?

Centering output often comes down to printing the correct number of leading spaces before the actual content.

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