Center-Aligned Pyramid Star Pattern in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2i - 1 stars

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:

Output
    *
   ***
  *****
 *******
*********
1

Complete C# Program

Fixed rows = 5 version:

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

1

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.

Setup
2

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.

Row control
3

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.

Centering
4

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.

Width
5

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.

Line break
=

Symmetric pyramid

Total stars (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.

2

Variation — User Input Version

Read rows with Console.ReadLine() and Convert.ToInt32:

C#
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 > 0 after 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.TryParse and a hollow pyramid variant

Avoid

  • Using 2 * i instead of 2 * i - 1 (even widths break the classic look)
  • Off-by-one on j <= rows - i vs <
  • Mixing tabs and spaces in console output

Key Takeaways

1

Row i: (rows - i) spaces, then (2i - 1) stars.

2

2i - 1 keeps each row width odd so the pyramid stays symmetric.

3

Same outer row index i as many triangle programs; the star formula defines the shape.

4

Time complexity O(n²) for n rows.

5

Reverse the outer loop to flip the pyramid without changing the inner loop bodies.

❓ Frequently Asked Questions

Odd lengths 1, 3, 5, … add one star on each side per row. 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.
Yes. Run i from rows down to 1 with the same inner loops; the first line is the widest.
O(n²) for 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.

All C# Star Patterns →
Did you know?

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.

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