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 only structural change is the outer loop: i runs 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

Complete C program for the inverted centered pyramid:

c
#include <stdio.h>

int main() {
    int rows = 5;
    int i, j, k;

    for (i = rows; i >= 1; --i) {
        for (j = 1; j <= rows - i; ++j) {
            printf(" ");
        }
        for (k = 1; k <= 2 * i - 1; ++k) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Declare Variables

rows is pyramid height. i is the current row index (counting down); j and k are space and star counters.

Setup
2

Outer Loop (Rows, Reverse)

for (i = rows; i >= 1; --i) prints the wide row first and the single-star row last.

Direction
3

Inner Loop — Spaces

for (j = 1; j <= rows - i; ++j) still prints rows - i spaces. As i drops, that count rises, indenting narrower rows.

Centering
4

Inner Loop — Stars

for (k = 1; k <= 2 * i - 1; ++k) prints an odd number of stars that shrinks as i shrinks.

Width
5

New Line

printf("\n") ends each row. Inner loops match the upright pyramid; only for (i = rows; i >= 1; --i) reverses row order so the widest line prints first.

Line break
=

Inverted pyramid

Same per-row length formula as the centered pyramid: (rows - i) + (2i - 1). Total time O(rows²), extra space O(1). Long lines use the same touch-scrollable glyph strip on mobile.

2

Variation — User Input Version

Read rows with scanf():

c
#include <stdio.h>

int main() {
    int rows;
    int i, j, k;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = rows; i >= 1; --i) {
        for (j = 1; j <= rows - i; ++j) {
            printf(" ");
        }
        for (k = 1; k <= 2 * i - 1; ++k) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Flip back to Program 5 by changing the outer loop to 1..rows
  • Validate rows > 0 after scanf()
  • Draw a hollow inverted pyramid (outline only)
  • Use digits instead of * for an inverted number pyramid
  • Compare mentally with Program 2 (inverted left triangle without centering)

Avoid

  • Changing the star bound to 2 * i (even widths break the usual symmetric look)
  • Using i++ on the outer loop by mistake (you need to decrement)
  • Altering the space formula without checking alignment
  • Omitting printf("\n") between rows
  • Ignoring scanf errors or negative 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

This is the centered analogue of “invert 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 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