Palindromic Number Pyramid in C

What You’ll Learn
How to print a palindromic number pyramid in C. Each row increases from 1 up to the row number and then decreases back to 1, so the row reads the same in both directions.
This pattern is a nice way to practice splitting work into an ascending part and a descending part.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
121
12321
1234321
123454321Complete C Program
We use two inner loops: one prints 1..i, the other prints i-1..1 to form the palindrome.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d", j);
}
for (j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer loop controls rows
for (i = 1; i <= rows; i++) prints one row per iteration.
First inner loop prints ascending
for (j = 1; j <= i; j++) prints 1..i.
Second inner loop prints descending
for (j = i - 1; j >= 1; j--) prints the mirror part back to 1.
Palindromic rows
Each row is symmetric, so it reads the same forwards and backwards.
Variation — User Input Version
Read the number of rows using scanf() and print the same pattern.
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d", j);
}
for (j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces between numbers:
printf("%d ", j) - Center the pyramid by printing leading spaces each row
- Print letters (A, B, C…) for an alphabet palindrome
- Create an inverted version by looping
idownward - Start from a custom number instead of 1
Avoid
- Printing the middle number twice (use
i-1in the second loop) - Forgetting the newline after each row
- Mixing loop ranges (keep the first loop ascending and the second descending)
- Not validating input when using
scanf()
Key Takeaways
Two inner loops create the palindrome: up then down.
The second loop starts at i-1 to avoid duplicating the center digit.
Total printed digits grow like 1+3+5+…, so the time complexity is O(n²).
The same approach works for palindromes with spaces, letters, or centered pyramids.
❓ Frequently Asked Questions
12321).1..i). The other prints the descending sequence (i-1..1) to mirror it.%d instead of %d in both loops.Explore More C Number Patterns!
Continue with more pyramids and symmetric patterns to sharpen your loop skills.
The total count of numbers printed in the first n rows is the sum of odd numbers: 1 + 3 + 5 + ... + (2n-1) = n².
12 people found this page helpful
