Palindromic Pattern with Decreasing Spaces in C

What You’ll Learn
How to print a palindromic number pattern where the inner gap shrinks each row. Each row has three parts: ascending numbers, spaces, then descending numbers.
The last row has no gap, so it becomes a continuous palindrome: 1234554321.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1 1
12 21
123 321
1234 4321
1234554321Complete C Program
We print 1..i, then (rows - i) * 2 spaces, then i..1.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d", j);
}
for (k = 1; k <= (5 - i) * 2; k++) {
printf(" ");
}
for (j = i; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer loop controls rows
for (i = 1; i <= rows; i++) prints 5 rows.
Print ascending numbers
for (j = 1; j <= i; j++) prints 1..i.
Print shrinking spaces
(rows - i) * 2 makes the gap shrink by 2 each row: 8, 6, 4, 2, 0.
Print descending numbers
for (j = i; j >= 1; j--) prints i..1 to mirror the left side.
Palindromic symmetry
Left and right parts mirror each other with a decreasing inner gap.
Variation — User Input Version
Read rows from the user and compute the spaces using (rows - i) * 2.
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int i, j, k;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d", j);
}
for (k = 1; k <= (rows - i) * 2; k++) {
printf(" ");
}
for (j = i; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Remove the spaces to make a continuous palindrome
- Change the spacing formula for different gap styles
- Use a custom gap character like
-or. - Print spaces on both sides to center the whole shape
- Use letters to create an alphabet palindrome
Avoid
- Forgetting the newline after each row
- Using a negative row count without validation
- Printing tabs (they don’t align consistently)
- Mixing ascending/descending loop bounds
Key Takeaways
Each row has three parts: numbers up, spaces, numbers down.
The gap is (rows - i) * 2, so it shrinks each row.
The result is palindromic because left and right parts mirror.
Nested loops + spacing control show up in many classic patterns.
❓ Frequently Asked Questions
1..i, then spaces, then i..1. The spaces decrease until the last row has none.Explore More C Number Patterns!
Keep going with more spacing and symmetry patterns to sharpen your logic.
Spacing is one of the most common pattern-printing tricks. By changing only the gap loop, you can transform the same numbers into diamonds, hourglasses, and many other shapes.
12 people found this page helpful
