- How to print a full symmetric number pyramid using nested while loops
- How to print spaces to center each row
- 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 control loop variables carefully in while loops
- How this pattern differs from a palindrome number pyramid
How the Symmetric Number Pyramid Works
A full symmetric number pyramid prints the same number repeatedly in each row. For example, row 1 contains one 1, row 2 contains two 2s, row 3 contains three 3s, and so on.
The pyramid is symmetric because spaces are printed before the numbers. The number of spaces decreases as the row number increases, which shifts each row toward the center.
This program uses nested while loops. The outer while loop controls the rows, the first inner while loop prints spaces, and the second inner while loop prints the numbers.
š” In one line: Print spaces to center the row, then print the current row number repeatedly, and move to the next line.
The Three Steps of Printing the Pyramid
| Step | While loop operation | Effect |
|---|---|---|
| 1. Print spaces | while (space <= rows - i) | Centers each row by printing decreasing spaces |
| 2. Print numbers | while (j <= i) | Prints the current row number i exactly i times |
| 3. Move to the next row | printf("\n"); | Starts a new line after completing the current row |
In this pattern, row 1 prints one 1, row 2 prints two 2s, row 3 prints three 3s, and row 4 prints four 4s. The value printed in each row is equal to the row number.
Unlike a palindrome pyramid, this pattern does not print increasing and decreasing numbers. It prints the same number repeatedly within each row.
Complete C Program
Program: print a full symmetric number pyramid using nested while 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;
}
i = 1;
while (i <= rows) {
/* Print leading spaces */
space = 1;
while (space <= rows - i) {
printf(" ");
space++;
}
/* Print the current row number */
j = 1;
while (j <= i) {
printf("%d ", i);
j++;
}
printf("\n");
i++;
}
return 0;
}
Program Output
Enter the number of rows: 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
A second run, with 4 rows:
Enter the number of rows: 4
1
2 2
3 3 3
4 4 4 4
The number of spaces decreases in every row, while the number of printed values increases. This creates the full symmetric pyramid shape.
Step-by-Step Explanation
Let us understand how the program prints the symmetric pyramid for 5 rows.
1. The outer while loop controls rows
The variable i starts at 1 and continues while i <= rows. Each iteration prints one complete row of the pyramid.
2. Printing spaces
The first inner while loop prints spaces. For row 1, it prints 4 groups of spaces. For row 2, it prints 3 groups, and this continues until row 5 prints no spaces.
3. Printing numbers
The second inner while loop prints the current row number. For row 3, it prints 3 three times. For row 5, it prints 5 five times.
4. Moving to the next line
After the number-printing loop finishes, printf("\n"); moves the output to the next line. Then i++ prepares the program for the next row.
5. Example trace for row 3
- The program prints spaces to align the row.
- The number-printing loop runs three times because
i = 3. - It prints
3 3 3. - The program prints a newline and moves to row 4.
6. Why spaces decrease
The condition space <= rows - i prints fewer spaces as i increases. This shift makes the pyramid appear centered and symmetrical.
7. Why the number equals the row number
In this pattern, the value printed in each row is the row number itself. Therefore, row 4 prints 4, row 5 prints 5, and so on.
8. Importance of resetting loop variables
The variables space and j must be reset at the beginning of every row. If they are not reset, the loops will not print the correct number of spaces or values.
9. While loop vs for loop
A for loop combines initialization, condition, and update in one line. A while loop separates them, so the programmer must initialize the variable before the loop and update it inside the loop. This program demonstrates that manual control.
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 a few integer variables and does not store the pyramid in an array.
šÆ Remember this: Use one while loop for rows, one for spaces, and one for numbers. Print the row number repeatedly, decrease the spaces row by row, and print a newline after each row.
Frequently Asked Questions
1. What is a full symmetric number pyramid?
A full symmetric number pyramid is a pyramid in which each row contains the same number repeated multiple times. Row 1 has one 1, row 2 has two 2s, row 3 has three 3s, and so on.
2. Why are nested while loops used?
Nested while loops are used because the program must handle rows, spaces, and numbers separately. The outer loop controls rows, while the inner loops print spaces and values.
3. Why must space and j be reset?
Both variables must be reset at the beginning of every row. Otherwise, their values from the previous row will affect the current row and produce an incorrect pattern.
4. How is this different from a palindrome pyramid?
A palindrome pyramid prints increasing and then decreasing numbers, such as 12321. A symmetric number pyramid prints the same number repeatedly, such as 333 in row 3.
5. Can this program be written using for loops?
Yes. The same output can be produced using nested for loops. The while-loop version is useful for understanding loop initialization, condition checking, and manual incrementing.
6. What is the time complexity of this program?
The time complexity is O(n2), because the total number of spaces and printed values grows with the square of the number of rows. Space complexity is O(1).
š” Tip: After implementing this program, try printing the same pyramid using stars, alphabets, or even numbers. The loop structure remains the same; only the printed value changes.