Centered Continuous Number Pyramid in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spacing + Nested Loops

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:

Output
    1 
  2 3 4
5 6 7 8 9
1

Complete C Program

We print spaces first to center the row, then print 2 * row - 1 numbers while keeping a continuous counter.

c
#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

1

Use a persistent counter

int count = 1; keeps numbers continuous across rows.

Setup
2

Outer loop controls rows

for (row = 1; row <= rows; row++) prints one line per row.

Row control
3

First inner loop prints spaces

rows - row controls leading spaces so each row is centered.

Centering
4

Second inner loop prints numbers

2 * row - 1 prints 1, 3, 5, … numbers each row while count increments continuously.

Number printing
=

Centered continuous number pyramid

Odd-length rows (1, 3, 5, …) form the pyramid; spacing makes it centered.

2

Variation — User Input Version

Let the user choose the number of rows using scanf():

c
#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

1

Use a persistent counter to keep numbers continuous across rows.

2

Print leading spaces to center each row.

3

Use 2 * row - 1 to get odd-length pyramid rows (1, 3, 5, …).

4

The same idea applies to star pyramids and alphabet pyramids.

❓ Frequently Asked Questions

It uses one continuous counter (no reset per row) and prints leading spaces to center each row. The number of items per row is odd (1, 3, 5, …), which forms a pyramid.
Spaces shift the starting position of the numbers so each row is centered. As the rows increase, the number of leading spaces decreases.
Yes. Remove the loop that prints the leading spaces. The numbers will start from the left margin.
O(n²) for n rows. Each row prints spaces and then an increasing count of numbers, so total work grows quadratically.

Explore More C Number Patterns!

Keep practicing nested loops with more pyramids and triangles.

All Number Patterns →
Did you know?

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.

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