×

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

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.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 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.

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.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 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.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.