Inverted Center-Aligned Pyramid Star Pattern in C#

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:
*********
*******
*****
***
*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 = 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
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.
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).
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.
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.
New line
Console.WriteLine() ends each row. Per-row character count is still (rows - i) + (2i - 1) = rows + i - 1.
Inverted pyramid
Total stars still n² 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.
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 = 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
Avoid
- Using
2 * iinstead of2 * i - 1for the star bound - Incrementing
ion the outer loop by mistake - Omitting
Console.WriteLine()between rows
Key Takeaways
Same inner loops as Program 5; reverse the outer loop to invert the pyramid.
As i decreases, rows - i grows and 2i - 1 shrinks—wider margin, fewer stars.
Printed star counts for rows = 5: 9, 7, 5, 3, 1.
Time complexity O(n²) for n rows.
Centered analogue of inverting the outer loop from Program 1 → Program 2.
❓ Frequently Asked Questions
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.i from 1 to rows (tip to base). Program 6 decrements i from rows to 1 (base to tip). Inner loops are unchanged.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.
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.
12 people found this page helpful
