Center-Aligned Pyramid Star Pattern in C#

What You'll Learn
How to print a center-aligned full pyramid in C#: on row i, print (rows - i) spaces, then (2 * i - 1) stars. The sequence 1, 3, 5, … widens the row by two stars each time so the shape is symmetric.
This is the usual next step after triangles such as Program 1 and Program 3.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see:
*
***
*****
*******
*********Complete C# Program
Fixed rows = 5 version:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Setup
rows is the pyramid height. i indexes rows 1…rows; j prints leading spaces; k prints the star run. All loops sit inside Main.
Outer loop (rows)
for (i = 1; i <= rows; i++) walks from one star on row 1 to a bottom row of 2 * rows - 1 stars.
Margin: Console.Write(" ")
for (j = 1; j <= rows - i; j++) { Console.Write(" "); } prints rows - i spaces so the odd-width star block sits centered under a fixed console font.
Stars: Console.Write("*")
for (k = 1; k <= 2 * i - 1; k++) { Console.Write("*"); } prints 1, 3, 5, …, 2*rows-1 asterisks—always an odd count so the pyramid has a single peak star.
New line
Console.WriteLine() ends the row after spaces and stars. Row i prints (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.
Symmetric pyramid
Total stars n² (sum of odd lengths 1 through 2n-1); O(n²) output for n = rows, O(1) extra space. Bottom row width 2n - 1 scrolls horizontally in the green preview on narrow screens.
Variation — User Input Version
Read rows with Console.ReadLine() and Convert.ToInt32:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j, k;
Console.Write("Enter the number of rows: ");
rows = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Validate
rows > 0after parsing - Use
for (i = rows; i >= 1; i--)for an inverted centered pyramid - Compare with Program 3: same space formula, different star count
- Try
int.TryParseand a hollow pyramid variant
Avoid
- Using
2 * iinstead of2 * i - 1(even widths break the classic look) - Off-by-one on
j <= rows - ivs< - Mixing tabs and spaces in console output
Key Takeaways
Row i: (rows - i) spaces, then (2i - 1) stars.
2i - 1 keeps each row width odd so the pyramid stays symmetric.
Same outer row index i as many triangle programs; the star formula defines the shape.
Time complexity O(n²) for n rows.
Reverse the outer loop to flip the pyramid without changing the inner loop bodies.
❓ Frequently Asked Questions
i stars alone would grow like a right triangle, not a balanced pyramid.rows - i spaces shrink as i grows, shifting the wider star runs left so the peak stays centered in a fixed-pitch grid.i from rows down to 1 with the same inner loops; the first line is the widest.n = rows: each row prints Θ(n) characters in the worst case.Explore More C# Star Patterns!
Inverted pyramids, diamonds, and hollow shapes use the same space-and-star building blocks.
With a fixed-pitch font, (rows - i) leading spaces plus an odd count 2*i-1 of stars lines up the peak and keeps each row symmetric about the center column of the base row.
12 people found this page helpful
