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: for each row i, print (rows - i) spaces, then (2 * i - 1) stars. The odd sequence 1, 3, 5, … makes each row wider by two stars so the shape is symmetric.

This is the pattern most courses use right after left- and right-aligned triangles (Program 1, Program 3).

⭐ Pattern Output

When you run the program with rows = 5, you’ll see:

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

Complete C Program

Complete C program for the centered pyramid:

c
#include <stdio.h>

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

    for (i = 1; i <= rows; ++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 sets pyramid height. i is the row; j prints spaces; k prints stars.

Setup
2

Outer Loop (Rows)

for (i = 1; i <= rows; ++i) grows the pyramid from the tip to the base.

Row control
3

Inner Loop — Spaces

for (j = 1; j <= rows - i; ++j) prints the left margin so the star block sits in the middle.

Centering
4

Inner Loop — Stars

for (k = 1; k <= 2 * i - 1; ++k) prints an odd number of stars: 1, 3, 5, …, 2*rows-1.

Width
5

New Line

printf("\n") finishes the row after margin and star block. Row i always emits (rows - i) + (2i - 1) = rows + i - 1 characters.

Line break
=

Symmetric pyramid

Odd star counts plus shrinking left margin keep the apex centered. Sum of row lengths is O(rows²); only a few integers are stored — O(1) extra space. Wide base rows scroll horizontally in the preview on small screens.

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

  • Validate rows > 0 after input
  • Invert the outer loop (i = rows; i >= 1; i--) for an upside-down pyramid
  • Print a hollow pyramid (only borders)
  • Replace * with increasing digits for a number pyramid
  • Compare with Program 3: same space formula rows - i, different star count

Avoid

  • Using 2 * i instead of 2 * i - 1 (even widths break the classic centered look)
  • Using j < rows - i when you need <= (off-by-one spacing)
  • Forgetting the newline after each row
  • Mixing tabs and spaces in output
  • Skipping bounds checks on scanf

Key Takeaways

1

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

2

2i - 1 forces odd widths so the pyramid stays symmetric.

3

Same outer row index i as many triangle programs; star formula is what changes the shape.

4

Time complexity O(n²) for n rows.

5

Reverse the outer loop to flip the pyramid upside down without changing the inner loop logic.

❓ Frequently Asked Questions

You need odd lengths 1, 3, 5, … so each row extends by one star on both sides. Using i stars would only add one star per row on one side and would not match the usual centered pyramid.
Printing rows - i spaces before the stars shifts the block left as i grows, keeping the middle of each odd-length star run aligned.
Yes. Use for (i = rows; i >= 1; --i) with the same space and star inner loops: the first line is the widest, then rows narrow toward the tip.
O(n²) for n rows: each row prints Θ(n) characters in the worst case, and there are n rows.

Explore More C Star Patterns!

Inverted pyramids, diamonds, and hollow shapes build on the same space-and-star ideas.

All 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