Palindrome Number Pattern in C

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

What You’ll Learn

How to print a number pattern where each row reads the same forwards and backwards (a palindrome). We build each row using an ascending loop followed by a descending loop.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
1
232
34543
4567654
567898765
1

Complete C Program

We print from i upward, then from just-before-the-peak downward back to i.

c
#include <stdio.h>

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

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

        m = m - 2;
        for (k = 1; k < i; k++)
            printf("%d", m--);

        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Start each row at i

m = i sets the starting digit for the row.

Setup
2

Ascending loop prints the left half

The loop runs i times and prints m++.

Ascend
3

Move back before the peak

m = m - 2 prevents printing the peak value twice.

Adjust
4

Descending loop mirrors back

The loop runs i-1 times and prints m-- to complete the palindrome.

Descend
=

Palindrome rows

Each 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 i, j, k, m;
    int rows;

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

    for (i = 1; i <= rows; i++) {
        m = i;

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

        m = m - 2;

        for (k = 1; k < i; k++) {
            printf("%d", m--);
        }

        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits with printf("%d ", ...)
  • Start from a different base value instead of i
  • Center-align each row by printing leading spaces

Avoid

  • Forgetting m = m - 2 (you’ll duplicate the peak)
  • Using rows <= 0 without validating input

Key Takeaways

1

Each row prints 2i-1 digits (up then down).

2

The descending part mirrors the ascending part, making a palindrome.

3

m = m - 2 prevents the peak digit from being printed twice.

4

Overall time complexity is O(n²) for n rows.

❓ Frequently Asked Questions

Because the ascending loop ends with m one step beyond the peak. Subtracting 2 moves it to the correct next value for the descending side.
Exactly 2i-1 digits.
O(n²) for n rows.

Explore More C Number Patterns!

Strengthen your loop logic with more palindrome and symmetry patterns.

All Number Patterns →
Did you know?

Row i goes up to 2i-1 at the peak, then mirrors back to i.

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