Centered Continuous Number Pyramid in C

What You’ll Learn
How to print a centered continuous number pyramid in C. Numbers are printed in a single increasing sequence (1, 2, 3, …) and do not reset each row.
The pyramid is centered by printing leading spaces before numbers on each row.
⭐ Pattern Output
For rows = 3, the pattern looks like this:
1
2 3 4
5 6 7 8 9Complete C Program
We print spaces first to center the row, then print 2 * row - 1 numbers while keeping a continuous counter.
#include <stdio.h>
int main() {
int row, space, num, count = 1;
for (row = 1; row <= 3; row++) {
for (space = 1; space <= 3 - row; space++) {
printf(" ");
}
for (num = 1; num <= 2 * row - 1; num++) {
printf("%d ", count);
count++;
}
printf("\n");
}
return 0;
}🧠 How It Works
Use a persistent counter
int count = 1; keeps numbers continuous across rows.
Outer loop controls rows
for (row = 1; row <= rows; row++) prints one line per row.
First inner loop prints spaces
rows - row controls leading spaces so each row is centered.
Second inner loop prints numbers
2 * row - 1 prints 1, 3, 5, … numbers each row while count increments continuously.
Centered continuous number pyramid
Odd-length rows (1, 3, 5, …) form the pyramid; spacing makes it centered.
Variation — User Input Version
Let the user choose the number of rows using scanf():
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int row, space, num, count = 1;
for (row = 1; row <= rows; row++) {
for (space = 1; space <= rows - row; space++) {
printf(" ");
}
for (num = 1; num <= 2 * row - 1; num++) {
printf("%d ", count);
count++;
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Accept row count from the user and validate it
- Use single spaces instead of double spaces for a tighter pyramid
- Start counting from a custom value (e.g., 10)
- Print letters instead of numbers (A, B, C…)
- Build an inverted pyramid by reversing row/space logic
Avoid
- Resetting the counter inside the row loop (breaks continuity)
- Forgetting the newline after each row
- Printing too many spaces (mis-centers the pyramid)
- Mixing up the formula for odd-length rows (
2 * row - 1)
Key Takeaways
Use a persistent counter to keep numbers continuous across rows.
Print leading spaces to center each row.
Use 2 * row - 1 to get odd-length pyramid rows (1, 3, 5, …).
The same idea applies to star pyramids and alphabet pyramids.
❓ Frequently Asked Questions
Explore More C Number Patterns!
Keep practicing nested loops with more pyramids and triangles.
The number of items in each row of a pyramid is usually odd (1, 3, 5, …). That’s why 2 * row - 1 is such a common formula in pyramid patterns.
12 people found this page helpful
