×

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

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

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

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.