C Language Program 07: Displaying a Pyramid Shape with Spaces and Asterisks

The pattern of the pyramid is actually an isosceles triangle. We take the number of lines displayed in the program as the height, using n to represent the number of lines, with the top layer as the first layer, incrementing downwards to the second layer, third layer, fourth layer, and so on, until the i-th layer.

The core rule of the pyramid is: the number of asterisks in each layer increases by odd numbers from top to bottom, specifically 1, 3, 5, 7, 9……, which ensures that the shape can expand layer by layer, ultimately forming a symmetrical triangle. This can be expressed in formula asthe i-th layer has 2 × i – 1 asterisks”..

Layer i

Number of Asterisks

Number of Lines n=5

First Layer

* 1

Second Layer

*** 3

Third Layer

***** 5

Fourth Layer

******* 7

Fifth Layer

********* 9

As shown in the figure above, having only asterisks is not enough, because if printed directly, they will all be squeezed to the left, failing to present the pyramid shape.

To center the pattern, a corresponding number of spaces must be arranged before the asterisks in each layer. The number of spaces decreases as the layer number increases, starting with the most at the top layer, decreasing layer by layer, until there are no spaces at the bottom layer. The specific rule isthe i-th layer needs n – i spaces”, which pushes the asterisks to the appropriate position, making the entire triangle symmetrical.

—-*

—***

–*****

-*******

*********

As shown in the figure above, since the output of spaces is not visually perceivable, we use “-” to represent spaces. Therefore, I see that the first layer of the pyramid with a height of 5 lines requires 4 spaces to push the asterisks to the 5th position of that line; the second layer requires 3 spaces to position the second asterisk at the 5th position of that line……

Following this pattern, when spaces and asterisks are combined, the visual effect of the pyramid appears: the top layer has the fewest asterisks, positioned in the center; the next layer increases the number of asterisks while decreasing the spaces, allowing the shape to naturally expand to the sides; ultimately, at the bottom layer, there are zero spaces and the most asterisks, forming the base of the triangle. The entire vertical centerline from the top layer to the base will pass through the center asterisk of each layer, ensuring symmetry.

Below is our program for a 5-layer pyramid:

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>int main(){    int i, j, k;    int n = 5;  // Control the number of lines    for (i = 1; i <= n; i++)    {        // Print leading spaces        for (j = 1; j <= n - i; j++)            printf(" ");             // Print asterisks        for (k = 1; k <= 2 * i - 1; k++)            printf("*"); // New line        printf("\n");    }    return 0;}

The output result shows

    *       ← 4 spaces + 1 asterisk   ***      ← 3 spaces + 3 asterisks  *****     ← 2 spaces + 5 asterisks *******    ← 1 space + 7 asterisks*********   ← 0 spaces + 9 asterisks

Leave a Comment