Square Number Pyramid in C

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

What You’ll Learn

How to print a centered square number pyramid in C using a running counter m. Each printed value is m*m, producing squares like 1, 4, 9, 16, and so on.

⭐ Pattern Output

For a maximum row width of 9 (odd), the pattern looks like this:

Output
                  1
              4   9  16
         25  36  49  64  81
    100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
1

Complete C Program

Print odd counts per row (1, 3, 5, 7, 9). Use spaces to center-align and %4d to align squares.

c
#include <stdio.h>

int main() {
    int i, j, k;
    int m = 1;

    for (i = 1; i <= 9; i += 2) {
        for (j = i; j < 9; ++j) {
            printf("  ");
        }
        for (k = 1; k <= i; ++k) {
            printf("%4d", m * m);
            ++m;
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Use a running counter

m starts at 1 and increases after each print. We output m*m to get squares.

Sequence
2

Print odd-length rows

i takes values 1, 3, 5, 7, 9 using i += 2.

Row width
3

Center with leading spaces

The first inner loop prints spaces so the pyramid stays centered as rows get wider.

Alignment
4

Format with %4d

%4d reserves 4 characters per value, keeping columns aligned.

Formatting
=

Square pyramid

Total prints for 5 rows are 1+3+5+7+9 = 25, so time complexity is O(n²).

2

Variation — User Input Version

Accept an odd maximum width from the user (like 9, 11, 13) and print the pyramid up to that size:

c
#include <stdio.h>

int main() {
    int i, j, k;
    int m = 1;
    int maxRow;

    printf("Enter the maximum row number (odd): ");
    scanf("%d", &maxRow);

    for (i = 1; i <= maxRow; i += 2) {
        for (j = i; j < maxRow; ++j) {
            printf("  ");
        }
        for (k = 1; k <= i; ++k) {
            printf("%4d", m * m);
            ++m;
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print cubes by changing m*m to m*m*m
  • Tune spacing by adjusting %4d and the leading-space loop
  • Start the sequence from a different number by changing m
  • Make it left-aligned by removing the leading spaces loop

Avoid

  • Using an even maxRow if you want perfectly symmetric odd-length rows
  • Removing %4d formatting while still expecting alignment
  • Resetting m inside the loops (it must be continuous)

Key Takeaways

1

Each printed value is a perfect square: m*m.

2

Row widths are odd numbers: 1, 3, 5, 7, 9.

3

%4d keeps multi-digit squares aligned.

4

Total prints follow for n rows.

❓ Frequently Asked Questions

Because the outer loop increases by 2 each time (i += 2), which generates odd row lengths and keeps the pyramid symmetric.
Squares quickly become 2–3 digits. %4d reserves enough space so the columns stay aligned.
Yes. Replace m * m with m * m * m.
O(n²) for n rows: total prints are 1+3+...+(2n-1) = n².

Explore More C Number Patterns!

Keep practicing with more pyramids, triangles, and number sequences.

All Number Patterns →
Did you know?

The sum of the first n odd numbers is . That’s why 1+3+5+7+9 = 25 prints for 5 rows.

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.

6 people found this page helpful