Palindromic Number Pattern in C (Center 1)

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:
1
212
32123
4321234
543212345Complete 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.
#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
Choose the row count
int rows = 5; sets how many lines will be printed.
Outer loop (rows)
for (i = 1; i <= rows; ++i) selects the current row value i.
First inner loop (descending)
for (j = i; j >= 1; --j) prints i, i-1, ..., 1.
Second inner loop (ascending)
for (j = 2; j <= i; ++j) prints 2, 3, ..., i, mirroring the left half while keeping 1 as the center.
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.
Variation — User Input Version
Let the user choose the number of rows using scanf():
#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
ifromrowsdown to 1 - Build a palindromic alphabet pattern using characters instead of numbers
Avoid
- Printing
1twice (start the second loop from2, not1) - Forgetting
printf("\n")after each row - Using invalid row inputs (consider validating
rows > 0)
Key Takeaways
Each row prints a palindrome with 1 at the center.
Use two inner loops: i..1 then 2..i.
Row length is 2i-1 and total prints are n².
The same idea works for alphabet palindromes and centered star pyramids.
❓ Frequently Asked Questions
1 in the middle (for example: 4321234).2 prevents printing 1 twice. The center 1 is already printed by the descending loop.printf("%d ", j) instead of printf("%d", j) in both inner loops.2i-1 numbers per row and the total is n².Explore More C Number Patterns!
Practice nested loops with pyramids, palindromes, Floyd’s triangle, and more.
The total count of printed digits across n rows is n² because you print the first n odd numbers: 1+3+5+...+(2n-1).
7 people found this page helpful
