Palindrome Number Pattern in C

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:
1
232
34543
4567654
567898765Complete C Program
We print from i upward, then from just-before-the-peak downward back to i.
#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
Start each row at i
m = i sets the starting digit for the row.
Ascending loop prints the left half
The loop runs i times and prints m++.
Move back before the peak
m = m - 2 prevents printing the peak value twice.
Descending loop mirrors back
The loop runs i-1 times and prints m-- to complete the palindrome.
Palindrome rows
Each row reads the same forward and backward.
Variation — User Input Version
Read the number of rows using scanf().
#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 <= 0without validating input
Key Takeaways
Each row prints 2i-1 digits (up then down).
The descending part mirrors the ascending part, making a palindrome.
m = m - 2 prevents the peak digit from being printed twice.
Overall time complexity is O(n²) for n rows.
❓ Frequently Asked Questions
m one step beyond the peak. Subtracting 2 moves it to the correct next value for the descending side.2i-1 digits.Explore More C Number Patterns!
Strengthen your loop logic with more palindrome and symmetry patterns.
Row i goes up to 2i-1 at the peak, then mirrors back to i.
12 people found this page helpful
