Centered Palindrome Number Pyramid in C

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

What You’ll Learn

How to print a centered pyramid where each row forms a palindrome: numbers increase from 1 up to the row number, then decrease back to 1.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
       1
     1 2 1
   1 2 3 2 1
 1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1

Complete C Program

We print leading spaces, then numbers ascending 1..i, then descending i-1..1.

c
#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf("  ");
        }

        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }

        for (int k = i - 1; k >= 1; k--) {
            printf("%d ", k);
        }

        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Print leading spaces

rows - i space groups keep the pyramid centered.

Align
2

Ascending part

Print numbers from 1 to i.

Up
3

Descending part

Print numbers from i-1 down to 1 to mirror the row.

Down
=

Centered palindrome pyramid

Every row reads the same forward and backward.

2

Variation — User Input Version

Read the number of rows using scanf().

c
#include <stdio.h>

int main() {
    int rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf("  ");
        }

        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }

        for (int k = i - 1; k >= 1; k--) {
            printf("%d ", k);
        }

        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Change spacing (one space vs two spaces) for a tighter pyramid
  • Remove the spacing loop to left-align the pyramid
  • Replace numbers with letters to create an alphabet pyramid

Avoid

  • Using rows <= 0 without validating input
  • Forgetting the descending loop (breaks the palindrome)

Key Takeaways

1

Leading spaces center each row.

2

Rows print ascending then descending numbers to form a palindrome.

3

Row i prints 2i-1 numbers.

4

Total runtime is O(n²).

❓ Frequently Asked Questions

Because i is already printed as the peak. Printing i-1..1 mirrors the left side without duplicating the center.
Remove the leading-space loop. You can also change printf(" ") to printf(" ") for tighter centering.
O(n²) for n rows.

Explore More C Number Patterns!

Try more pyramid and palindrome patterns to strengthen your nested-loop skills.

All Number Patterns →
Did you know?

The widest row prints 2*rows - 1 numbers. For rows=5, that’s 9 values.

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