Center-Aligned Pyramid Star Pattern in C

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:
*
***
*****
*******
*********Complete C Program
Complete C program for the centered pyramid:
#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
Declare Variables
rows sets pyramid height. i is the row; j prints spaces; k prints stars.
Outer Loop (Rows)
for (i = 1; i <= rows; ++i) grows the pyramid from the tip to the base.
Inner Loop — Spaces
for (j = 1; j <= rows - i; ++j) prints the left margin so the star block sits in the middle.
Inner Loop — Stars
for (k = 1; k <= 2 * i - 1; ++k) prints an odd number of stars: 1, 3, 5, …, 2*rows-1.
New Line
printf("\n") finishes the row after margin and star block. Row i always emits (rows - i) + (2i - 1) = rows + i - 1 characters.
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.
Variation — User Input Version
Read rows with scanf():
#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 > 0after 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 * iinstead of2 * i - 1(even widths break the classic centered look) - Using
j < rows - iwhen 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
Row i: (rows - i) spaces, then (2i - 1) stars.
2i - 1 forces odd widths so the pyramid stays symmetric.
Same outer row index i as many triangle programs; star formula is what changes the shape.
Time complexity O(n²) for n rows.
Reverse the outer loop to flip the pyramid upside down without changing the inner loop logic.
❓ Frequently Asked Questions
i stars would only add one star per row on one side and would not match the usual centered pyramid.rows - i spaces before the stars shifts the block left as i grows, keeping the middle of each odd-length star run aligned.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.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.
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.
12 people found this page helpful
