Pyramid Pattern in C
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:
- accepts the number of rows from the user to form a pyramid structure.
- Iterates the loop until the number of rows specified by the user:
- Displays 1 star in the first row.
- 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 *
#include <stdio.h> #include <conio.h> #include <string.h> int main () { int i; int j; int rows; //initializing the variables required for further steps printf (“ Enter the number of rows of the required pyramid: \n”); /* specifies the outer loop to handle the number of rows by the user*/ scanf (“%d \n”, &rows); //reads the number of rows for (i = 1; i <= rows; ++i) /* inner loop to handle the number of columns and values will change according to the outer loop */ { for (j = 1; j <= i; ++j) { printf (“ * ”); // outputs and prints the stars } printf (“\n”); // escape sequence to get a proper structure of the pyramid } return 0; }
Output:
Enter the number of rows of the required pyramid: 5
*
* *
* * *
* * * *
* * * * *
- Inverted half pyramid of *
#include <stdio.h> #include <conio.h> #include <string.h> int main () { int i; int j; int rows; //initializing the variables required for further steps printf (“ Enter the number of rows of the required pyramid: \n”); /* specifies the outer loop to handle the number of rows by the user*/ scanf (“%d \n”, &rows); //reads the number of rows for (i =rows; i >= 1; --i) /* inner loop to handle the number of columns and values will change according to the outer loop */ { for (j = 1; j <= i; ++j) { printf (“ * ”); // outputs and prints the stars } printf (“\n”); // escape sequence to get a proper structure of the pyramid } return 0; }
Output:
Enter the number of rows of the required pyramid: 5
* * * * *
* * * *
* * *
* *
*
- Full pyramid of *
#include <stdio.h> #include <conio.h> #include <string.h> int main() { int i, j, n, k = 0; printf (“Enter the number of rows for the pyramid: \n”); scanf(“%d”,&n); printf (“The pyramid pattern for the given rows are:”); printf (“ \n \n”); for(i = 1; i <= n; ++i, k = 0) { for(j = 1; j <= n – i; ++j) { printf(“ ”); } while (k != 2 * i - 1) { printf(“ * ”); ++k; } printf(“\n”); } return 0; }
Output:
Enter the number of rows for the pyramid: 5
The pyramid pattern for the given rows are:
*
* *
* * *
* * * *
* * * * *
- Inverted full pyramid of *
#include <stdio.h> #include <conio.h> #include <string.h> int main() { int i, j, n, k = 0; printf (“Enter the number of rows for the pyramid: \n”); scanf(“%d”,&n); printf (“The pyramid pattern for the given rows are:”); printf (“ \n \n”); for(i = n; i >= 1; --i) { for(j = 0; j < n - i; ++j) printf(“ ”); for(j = i; j <= 2*i - 1; ++j) printf(“ * ”); for(j = 0; j < i - 1; ++j) printf(“ * ”); printf(“\n”); } return 0; }
Output:
Enter the number of rows for the pyramid: 5
The pyramid pattern for the given rows are:
* * * * *
* * * *
* * *
* *
*
- 180 degree rotated half pyramid
#include <stdio.h> #include <conio.h> #include <string.h> void pyramid (int n) { int k = 2 * n - 2; for (int i = 0; i < n; i++) /* Outer loop to handle number of rows*/ { for (int j = 0; j < k; j++) /*Inner loop to handle number spaces*/ printf (“ ”); k = k - 2; /* Decrementing k after each loop */ for (int j = 0; j <= i; j++)/*Inner loop to handle number of columns*/ { printf (“ * ”);/* Printing stars */ } printf (“\n”); /*Ending line after each row*/ } } int main() { int n; printf (“Enter the number of rows for the pyramid: \n”); scanf(“%d”,&n); printf (“The pyramid pattern for the given rows are:”); printf (“ \n \n”); pyramid(n); /* Function Call*/ return 0; }
Output:
Enter the number of rows for the pyramid: 5
The pyramid pattern for the given rows are:
*
* *
* * *
* * * *
* * * * *
- 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.
- 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.
- 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 ( * ).
- 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 ( * ).
- The iteration loops until the end condition is met.
- The print statement present at the end is responsible for ending the line after each row.