Select Page

Pyramid Pattern in C

A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected at the tip, which is called the apex; the base and the apex form a triangle. The shape of its base basically describes a pyramid; for example, a triangular pyramid has a base of a triangle, a pentagonal pyramid has a base of a pentagon, etc. A pyramid with an ‘n’ sided base will always have ‘n + 1’ vertices, ‘n + 1’ faces, and ‘2 n’ edges. All the pyramids are ‘self-dual’. Several areas of mathematics have the notion of ‘dual’ that can be applied to many objects of the particular area. Whenever an object is said to self-dual, then it is equal to its own dual.

In the C programming language, a pyramid pattern is a 2 D (2 Dimensional) figure that will be obtained with the help of loops such as, for, if – else, while – do-while, and break – continue. Using algorithms to accept the number of rows given by the user to form a pyramid shape, iterating the loop until the number of rows specified by the user is satisfied to get the particular structure of the pyramid will be practiced.

The algorithm:

1. accepts the number of rows from the user to form a pyramid structure.
2. Iterates the loop until the number of rows specified by the user:
3. Displays 1 star in the first row.
4. Increase the number of stars based on the rows specified by the user.

Programs to print pyramid pattern in different ways:

• Half pyramid pattern of a *

Output:

Enter the number of rows of the required pyramid: 5

*

* *

* * *

* * * *

* * * * *

• Inverted half pyramid of *

Output:

Enter the number of rows of the required pyramid: 5

* * * * *

* * * *

* * *

* *

*

• Full pyramid of *

Output:

Enter the number of rows for the pyramid: 5

The pyramid pattern for the given rows are:

*

* *

* * *

* * * *

* * * * *

• Inverted full pyramid of *

Output:

Enter the number of rows for the pyramid: 5

The pyramid pattern for the given rows are:

* * * * *

* * * *

* * *

* *

*

• 180 degree rotated half pyramid

Output:

Enter the number of rows for the pyramid: 5

The pyramid pattern for the given rows are:

*

* *

* * *

* * * *

* * * * *

1. In the code snippets given, integer ‘n’ is initialized, which takes in the value of rows for the pattern. Since n = 5, in all the examples, it can be inferred that the range of outer for loop iterates from 0 to 4.
2. Iteration of the inner loop always depends on the iteration of the outer loop. It can be seen that the inner loop is responsible for the number of columns to be printed.
3. When the value of i is 0, i.e., in the first iteration, i will increase to 1, hence becoming 0 + 1, during which the inner loop will print one star ( * ).
4. When i = 1, in the second iteration, i will be 1, and it will again be increased by 1, hence 1 + 1, during which the inner loop will print another star ( * ).
5. The iteration loops until the end condition is met.
6. The print statement present at the end is responsible for ending the line after each row.