Palindromic Number Pattern in C (Center 1)

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

What You’ll Learn

How to print a palindromic number pattern in C where every row has 1 at the center. For each row i, print i..1 (descending) and then 2..i (ascending) to form a palindrome like 4321234.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
212
32123
4321234
543212345
1

Complete C Program

Use two inner loops: one for descending i..1, one for ascending 2..i. This keeps 1 at the center in every row.

c
#include <stdio.h>

int main() {
    int rows = 5;
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Choose the row count

int rows = 5; sets how many lines will be printed.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; ++i) selects the current row value i.

Row control
3

First inner loop (descending)

for (j = i; j >= 1; --j) prints i, i-1, ..., 1.

Left half
4

Second inner loop (ascending)

for (j = 2; j <= i; ++j) prints 2, 3, ..., i, mirroring the left half while keeping 1 as the center.

Right half
=

Palindromic rows with center 1

Row length is 2i-1. Total printed numbers are 1+3+…+(2n-1) = n², so time complexity is O(n²) for n rows.

2

Variation — User Input Version

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

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits with printf("%d ", j)
  • Center-align by printing leading spaces before each row
  • Print the inverted version by looping i from rows down to 1
  • Build a palindromic alphabet pattern using characters instead of numbers

Avoid

  • Printing 1 twice (start the second loop from 2, not 1)
  • Forgetting printf("\n") after each row
  • Using invalid row inputs (consider validating rows > 0)

Key Takeaways

1

Each row prints a palindrome with 1 at the center.

2

Use two inner loops: i..1 then 2..i.

3

Row length is 2i-1 and total prints are .

4

The same idea works for alphabet palindromes and centered star pyramids.

❓ Frequently Asked Questions

Each row reads the same forward and backward, with 1 in the middle (for example: 4321234).
Starting from 2 prevents printing 1 twice. The center 1 is already printed by the descending loop.
Use printf("%d ", j) instead of printf("%d", j) in both inner loops.
O(n²) for n rows, since you print 2i-1 numbers per row and the total is .

Explore More C Number Patterns!

Practice nested loops with pyramids, palindromes, Floyd’s triangle, and more.

All Number Patterns →
Did you know?

The total count of printed digits across n rows is because you print the first n odd numbers: 1+3+5+...+(2n-1).

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.

7 people found this page helpful