Palindromic Number Pyramid in C

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

What You’ll Learn

How to print a palindromic number pyramid in C. Each row increases from 1 up to the row number and then decreases back to 1, so the row reads the same in both directions.

This pattern is a nice way to practice splitting work into an ascending part and a descending part.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
121
12321
1234321
123454321
1

Complete C Program

We use two inner loops: one prints 1..i, the other prints i-1..1 to form the palindrome.

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Outer loop controls rows

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

Row control
2

First inner loop prints ascending

for (j = 1; j <= i; j++) prints 1..i.

Ascending
3

Second inner loop prints descending

for (j = i - 1; j >= 1; j--) prints the mirror part back to 1.

Descending
=

Palindromic rows

Each row is symmetric, so it reads the same forwards and backwards.

2

Variation — User Input Version

Read the number of rows using scanf() and print the same pattern.

c
#include <stdio.h>

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

    int i, j;
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between numbers: printf("%d ", j)
  • Center the pyramid by printing leading spaces each row
  • Print letters (A, B, C…) for an alphabet palindrome
  • Create an inverted version by looping i downward
  • Start from a custom number instead of 1

Avoid

  • Printing the middle number twice (use i-1 in the second loop)
  • Forgetting the newline after each row
  • Mixing loop ranges (keep the first loop ascending and the second descending)
  • Not validating input when using scanf()

Key Takeaways

1

Two inner loops create the palindrome: up then down.

2

The second loop starts at i-1 to avoid duplicating the center digit.

3

Total printed digits grow like 1+3+5+…, so the time complexity is O(n²).

4

The same approach works for palindromes with spaces, letters, or centered pyramids.

❓ Frequently Asked Questions

Each row is a palindrome: it increases from 1 to the row number and then decreases back to 1 (for example: 12321).
One loop prints the ascending sequence (1..i). The other prints the descending sequence (i-1..1) to mirror it.
Yes. Print %d instead of %d in both loops.
O(n²) for n rows because the total number of printed digits grows quadratically.

Explore More C Number Patterns!

Continue with more pyramids and symmetric patterns to sharpen your loop skills.

All Number Patterns →
Did you know?

The total count of numbers printed in the first n rows is the sum of odd numbers: 1 + 3 + 5 + ... + (2n-1) = n².

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