×

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:

  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 *
 #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:

 *

         * *

                  * * *

   * * * *

* * * * *

  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.

Related Topics

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.