×

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

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

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

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

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

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

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

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.