Centered Continuous Number Pyramid in C#

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:
1
2 3 4
5 6 7 8 9Complete 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++.
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
Initialize the counter
k = 1 stores the next number to print.
Outer loop chooses odd row width
i takes 1, 3, 5 so the pyramid grows by 2 numbers per row.
Print spaces then numbers
When j > i we print a space; otherwise we print k++ followed by a space.
Centered pyramid
Numbers increase continuously because k never resets.
Variation — Custom Max Width
Let the user choose the maximum odd width (like 7 or 9) and generate the pyramid accordingly:
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
keach 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
Use a counter k to continue numbering across rows.
Odd row widths (1,3,5,...) create a pyramid shape.
Leading spaces align the numbers to the center.
The same idea works for other centered patterns.
❓ Frequently Asked Questions
j > i. For i = 1 and max width 5, three positions are spaces, so the single number appears centered.k = 1 inside the outer loop so it resets for every row.Explore More C# Number Patterns!
Centered patterns are great practice for combining loops with alignment logic.
Centering output often comes down to printing the correct number of leading spaces before the actual content.
12 people found this page helpful
