Inverted Center-Aligned Pyramid Star Pattern in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Outer loop reversed

What You'll Learn

How to print an upside-down centered pyramid in C#. The inner loops match Program 5: (rows - i) spaces, then (2 * i - 1) stars. The outer loop runs i from rows down to 1, so the widest row prints first.

With rows = 5, star counts per printed line are 9, 7, 5, 3, 1.

⭐ 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 = rows; i >= 1; 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 height. i will count rows from rows down to 1; j prints spaces; k prints stars. Same inner bodies as an upright pyramid, only the outer i loop runs backward.

Setup
2

Outer loop (rows, reverse)

for (i = rows; i >= 1; i--) starts with i == rows (widest line, 2*rows-1 stars, no left margin) and ends at i == 1 (one star, rows - 1 spaces).

Direction
3

Margin: Console.Write(" ")

for (j = 1; j <= rows - i; j++) { Console.Write(" "); } prints rows - i spaces. As i decreases each row, that count grows so the shrinking star band stays centered.

Centering
4

Stars: Console.Write("*")

for (k = 1; k <= 2 * i - 1; k++) { Console.Write("*"); } prints an odd number of stars that steps down 9, 7, 5, … for rows = 5. Same formula as the upright pyramid; only i’s sequence changed.

Width
5

New line

Console.WriteLine() ends each row. Per-row character count is still (rows - i) + (2i - 1) = rows + i - 1.

Line break
=

Inverted pyramid

Total stars still over n = rows rows; O(n²) time, O(1) extra space. The first line is the widest—it scrolls sideways in the green preview on small viewports.

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 = rows; i >= 1; 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

  • Flip back to Program 5 by using for (i = 1; i <= rows; i++)
  • Validate rows > 0 after parsing
  • Try a hollow inverted pyramid (outline only)
  • Compare with Program 2 (inverted left triangle without centering)

Avoid

  • Using 2 * i instead of 2 * i - 1 for the star bound
  • Incrementing i on the outer loop by mistake
  • Omitting Console.WriteLine() between rows

Key Takeaways

1

Same inner loops as Program 5; reverse the outer loop to invert the pyramid.

2

As i decreases, rows - i grows and 2i - 1 shrinks—wider margin, fewer stars.

3

Printed star counts for rows = 5: 9, 7, 5, 3, 1.

4

Time complexity O(n²) for n rows.

5

Centered analogue of inverting the outer loop from Program 1 → Program 2.

❓ Frequently Asked Questions

With for (i = rows; i >= 1; i--), the first iteration uses the largest i, so you print the longest line first. Later iterations use smaller i, so 2*i-1 falls while rows-i rises.
rows - i grows when i gets smaller. That extra left padding keeps shorter star runs centered under the first (widest) row.
Program 5 increments i from 1 to rows (tip to base). Program 6 decrements i from rows to 1 (base to tip). Inner loops are unchanged.
O(n²) for n rows: same per-row print totals as Program 5, only iteration order differs.

Explore More C# Star Patterns!

Diamonds and hollow shapes combine upright and inverted halves you already know.

All C# Star Patterns →
Did you know?

Program 6 is exactly Program 5 with the outer loop reversed—the same relationship as Program 1 versus Program 2, but with centered odd-width rows instead of a left-aligned wedge.

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