- How to print a number palindrome pyramid using nested loops
- How to print increasing numbers from 1 to the current row
- How to print decreasing numbers from the current row back to 1
- How to print spaces to make the pyramid symmetrical
- Why the middle number is printed only once
- How nested loops create symmetric patterns
How the Number Palindrome Pyramid Works
A number palindrome pyramid is a symmetric pyramid in which each row reads the same from left to right and right to left. The first row is 1, the second row is 121, the third row is 12321, and so on.
Each row has two parts: an increasing part and a decreasing part. For example, row 4 contains 123 followed by 21. Together, they form 12321.
The program uses three loops for each row: one loop prints spaces, one loop prints increasing numbers, and another loop prints decreasing numbers. The outer loop controls the rows.
💡 In one line: Print spaces, then numbers from 1 to the current row, and finally numbers from the current row minus 1 back to 1.
The Three Steps of Printing the Pyramid
| Step | Loop operation | Effect |
|---|---|---|
| 1. Print spaces | for (space = 1; space <= rows - i; space++) | Centers each row and creates the pyramid shape |
| 2. Print increasing numbers | for (j = 1; j <= i; j++) | Prints 1, 2, 3, and so on up to the current row number |
| 3. Print decreasing numbers | for (j = i - 1; j >= 1; j--) | Prints the numbers in reverse order back to 1 |
The increasing part of row 4 is 1234. The decreasing part is 321. When joined together, they form 1234321.
The middle number is printed only once. This is why the decreasing loop starts from i - 1, not from i. Otherwise, the middle number would be printed twice.
Complete C Program
Program: print a number palindrome pyramid using nested loops
#include <stdio.h>
int main(void) {
int rows, i, j, space;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++) {
/* Print leading spaces */
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
/* Print increasing numbers */
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
/* Print decreasing numbers */
for (j = i - 1; j >= 1; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Program Output
Enter the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
A second run, with 4 rows:
Enter the number of rows: 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Each row is symmetrical. For example, row 4 prints 1 2 3 4 3 2 1, which reads the same from both directions.
Step-by-Step Explanation
Let us understand how the program prints the palindrome pyramid for 5 rows.
1. The outer loop controls rows
The outer loop for (i = 1; i <= rows; i++) runs from row 1 to row 5. Each iteration prints one complete row of the pyramid.
2. Printing spaces
The first inner loop prints spaces before the numbers. The number of spaces decreases as the row number increases, which gives the output its pyramid shape.
3. Printing increasing numbers
The second inner loop prints numbers from 1 to i. For row 4, it prints 1 2 3 4.
4. Printing decreasing numbers
The third inner loop starts from i - 1 and prints numbers down to 1. For row 4, it prints 3 2 1. The value 4 is not repeated because it is already the middle number.
5. Example trace for row 4
- Spaces: The program prints spaces to align the row.
- Increasing part:
1 2 3 4. - Decreasing part:
3 2 1. - Complete row:
1 2 3 4 3 2 1.
6. Why the decreasing loop starts from i - 1
The increasing loop already prints the middle number, which is i. Therefore, the decreasing loop begins from i - 1 to avoid printing the middle number twice.
7. Why spaces are needed
Spaces shift each row toward the right. Without spaces, the numbers would start from the left edge and the output would not look like a pyramid.
8. Why the pattern is a palindrome
A palindrome reads the same forward and backward. Row 4, 1234321, reads the same from left to right and right to left. This is why the pattern is called a number palindrome pyramid.
9. Number of values in each row
Row i contains \(2i - 1\) numbers. For example, row 1 has 1 number, row 2 has 3 numbers, row 3 has 5 numbers, and row 4 has 7 numbers.
10. Complexity summary
Time complexity is O(n2), because the program prints a growing number of spaces and values for each row. Space complexity is O(1), because the program uses only loop variables and does not store the pyramid in an array.
🎯 Remember this: Print spaces first, then numbers from 1 to i, and finally numbers from i - 1 back to 1. This creates the symmetric palindrome pyramid.
Frequently Asked Questions
1. What is a number palindrome pyramid?
A number palindrome pyramid is a pyramid in which every row reads the same from left to right and right to left. Examples include 1, 121, 12321, and 1234321.
2. Why does the decreasing loop start from i - 1?
The increasing loop already prints the middle number i. Starting the decreasing loop from i - 1 prevents the middle number from being printed twice.
3. Why are spaces printed before the numbers?
Spaces shift each row to the right, which creates the pyramid shape. The number of spaces decreases as the row number increases.
4. How many numbers are printed in row i?
Row i contains \(2i - 1\) numbers. For example, row 3 contains 5 numbers: 1, 2, 3, 2, and 1.
5. Can this pattern be printed using only two loops?
Yes. You can use one loop for spaces and another loop that prints the increasing and decreasing parts together. However, using three separate loops makes the logic easier to understand for beginners.
6. What is the time complexity of this program?
The time complexity is O(n2), because the total number of spaces and numbers grows with the square of the number of rows. Space complexity is O(1), since no array is used.
💡 Tip: After implementing this program, try printing the same pyramid using alphabets, such as A, ABA, ABCBA. The loop structure remains the same; only the printed character changes.