Pyramid Patterns in C

What is a Pyramid Pattern?

A pyramid pattern in a two-dimensional plane is equal to an equilateral triangle. There are many variations of these pyramid patterns such as half pyramid, inverted pyramid, and diamond shape. Let us see how we can draw all these shapes using C language and understand the process.

Problem Description

Write a C program that prints a pyramid patterns upon receiving number of rows as input. Pyramid patterns like full pyramid, half pyramid, inverted pyramid, left aligned pyramid and combine two half pyramids.

Problem Solution

Algorithm:
1. Take the number of rows as input.
2. Store the number in a variable.
3. Inside a loop, print the spaces and then the asterisks.
4. Repeat the process until the number is equal to 0.
5. Exit.

There are various ways to print a pyramid pattern in C language. Let’s take a detailed look at all the approaches to print a pyramid pattern in C.

Method 1: Full Pyramid

In this pattern design, we simply need to print an equilateral triangle. And to print this design pattern in C, again, we need a nested loop. This is how a pyramid pattern looks when building units for the pattern are * asterisk symbol.
Example of Full Pyramid:

advertisement
advertisement
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * *
Program/Source Code

Here is source code of the C Program to print full pyramid. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Full Pyramid
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d", &rows);
  14.  
  15.     //full pyramid
  16.     printf("****--Full Pyramid---****\n");
  17.     for(int i=0; i<rows; i++)
  18.     {
  19.         for(int l=0;l<rows-1-i;l++)
  20.         {
  21.             printf(" ");
  22.         }
  23.         for(int j=0; j<=i;j++)
  24.         {
  25.             printf("%c ",'*');
  26.         }
  27.         printf("\n");
  28.     }
  29.     printf("\n");
  30.     return 0;
  31. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. Run a loop that runs rows-number of times, and we can see that as we progress along the pattern, the number of * symbols increases by one.
3. The number of * symbols on each line equals the line number. As a result, we must print as many stars as line numbers.
4. We also need to print spaces to gain symmetry, for that we print the rows-line number of spaces.
5. But there is a catch in this pattern. The nested loop contains two inner for loops i and j. Where i will be responsible for printing the white space and j will print the star for the pattern.

Note: Join free Sanfoundry classes at Telegram or Youtube

Time Complexity: O(n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity is O(n2). (where n is the number of rows).

Space Complexity: O(1)
It uses constant space, so the space complexity is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “6” to print the full pyramid pattern with stars.

Enter the number of rows: 6
****--Full Pyramid---****
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * *

advertisement
Method 2: Full Pyramid with Alphabets as Building Units

We can also print the alphabet in place of the * sign by using the %c format specifier, which converts the integer constant to alphabets.

Program/Source Code

Here is source code of the C Program to print full pyramid with alphabets. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Full Pyramid with alphabets
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d",&rows);
  14.  
  15.     printf("****--full pyramid---****\n");
  16.     for(int i=0; i<rows; i++)
  17.     {
  18.  
  19. 	// loop to print the row-line number of spaces
  20.         for(spaces=0;spaces<rows-1-i;spaces++)
  21.         {
  22.             printf(" ");
  23.         }
  24.  
  25. 	//loop to print the characters
  26.         for(int j=0; j<=i;j++)
  27.         {
  28.             printf("%c ",'a'+j);
  29.         }
  30.         printf("\n");
  31.     }
  32.     printf("\n");
  33.     return 0;
  34. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. ‘a’ is an ASCII character with a value = 97 when we add 97 to 1 and use the %c format specifier the output will be ‘b’ thus we can print alphabets like that.
3. Run a loop that runs rows-number of times, and we can see that as we progress along the pattern, the number of * symbols increases by one.
4. The number of * symbols on each line equals the line number. As a result, we must print as many stars as line numbers.
5. We also need to print spaces to gain symmetry, for that we print the rows-line number of spaces.

Time Complexity: O(n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity is O(n2). (where n is the number of rows).

advertisement

Space Complexity: O(1)
It uses constant space, so the space complexity is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “8” to print the full pyramid pattern with alphabets.

Enter the number of rows: 8
****--full pyramid---****
       a 
      a b 
     a b c 
    a b c d 
   a b c d e 
  a b c d e f 
 a b c d e f g 
a b c d e f g h

Method 3: Half Pyramid

Half Pyramid is a pattern that looks like a right-angle triangle. To design this pattern, simply skip the step of printing spaces and only print the characters.

Program/Source Code

Here is source code of the C Program to print half pyramid. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Half Pyramid
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d",&rows);
  14.     printf("\n");
  15.     printf("****--half pyramid---****\n");
  16.     for(int i=0; i<rows; i++)
  17.     {
  18.         for(int j=0; j<=i;j++)
  19.         {
  20.             printf("* ");
  21.         }
  22.         printf("\n");
  23.     }
  24.     return 0;
  25. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. Run a loop that runs rows-number of times, and we can see that as we progress along the pattern, the number of * symbols increases by one.
3. A nested loop is used to print the stars in the half Pyramid pattern.
4. The number of * symbols on each line equals the line number. As a result, we must print as many stars as line numbers.

Time Complexity: O(n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity of half pyramid is O(n2). (where n is the number of rows).

Space Complexity: O(1)
It uses constant space, so the space complexity is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “6” to print the half pyramid pattern.

Enter the number of rows : 6
****--half pyramid---****
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * *

Method 4: Inverted Pyramid

The logic for this pattern program in C is similar to the full pyramid program the only change is that instead of utilising an increment for loop, we will use a decrement outer for the loop statement.

Program/Source Code

Here is source code of the C Program to print inverted pyramid. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Inverted Pyramid
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d",&rows);
  14.     printf("****--Inverted Pyramid---****\n\n");
  15.     for(int i=0; i<rows; i++)
  16.     {
  17.         for(int spaces=0;spaces<i; spaces++)
  18.         {
  19.             printf(" ");
  20.         }
  21.         for(int j=0;j<rows-i;j++)
  22.         {
  23.             printf("* ");
  24.         }
  25.         printf("\n");
  26.     }
  27.     printf("\n");
  28.     return 0;
  29. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. Run a loop that runs rows-number of times, and we can see that as we progress along the pattern, the number of * symbols decreases by one.
3. The number of * symbols on each line equals the line number. As a result, we must print as many stars as line numbers.
4. We also need to print spaces to gain symmetry, for that we print the rows-line number of spaces.
5. Here, we adjust our condition for printing characters such that we print the number of characters in the first row, then decrease the number of characters by one, and so on until we reach the last row.

Time Complexity: O(n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity of inverted pyramid is O(n2). (where n is the number of rows).

Space Complexity: O(1)
It uses constant space, so the space complexity of inverted pyramid is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “6” to print the inverted pyramid pattern.

Enter the number of rows: 6
****--Inverted Pyramid---****
 
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     *

Method 5: Inverted Half Pyramid

The logic for this pattern program in C is similar to the half pyramid program the only change is that instead of utilising an increment for loop, we will use a decrement outer for the loop statement.

Program/Source Code

Here is source code of the C Program to print inverted half pyramid. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Inverted Half Pyramid
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows: ");
  13.     scanf("%d",&rows);
  14.     printf("****--Half Inverted Pyramid---****\n\n");
  15.     for(int i=0; i<rows; i++)
  16.     {
  17.         for(int spaces =0;spaces<i;spaces++)
  18.         {
  19.             printf("  ");
  20.         }
  21.         for(int j=0;j<rows-i;j++)
  22.         {
  23.             printf("* ");
  24.         }
  25.         printf("\n");
  26.     }
  27.     printf("\n");
  28.     return 0;
  29. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. In the inverted half pyramid, we run an outer loop that iterates from 1 to the count of rows entered by the user.
3. In the inner loop, we iterate the loop to print the characters and spaces.
4. The First inner loop is to print the spaces and the second inner loop is to print the characters.
5. The loop that prints the spaces starts at 0 and keeps incrementing until the outer loop is completed.
6. The loop to print characters in the pattern runs from the count of rows to 1 as the number of characters keeps on decreasing as we move down the row.

Time Complexity: O(2n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity of inverted half pyramid is O(2n2). (where n is the number of rows).

Space Complexity: O(1)
It uses constant space, so the space complexity is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “9” to print the inveted half pyramid pattern.

Enter the number of rows: 9
****--Half Inverted Pyramid---****
 
* * * * * * * * * 
  * * * * * * * * 
    * * * * * * * 
      * * * * * * 
        * * * * * 
          * * * * 
            * * * 
              * * 
                *

Method 6: Left Aligned Pyramid Pattern

The logic for this pattern program in C is similar to the full pyramid program the only change is that instead of utilising an increment for loop, we will use a decrement outer for the loop statement.

Program/Source Code

Here is source code of the C Program to print left aligned pyramid. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Left Aligned Pyramid
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d",&rows);
  14.     printf("\n");
  15.  
  16.     //left pyramid....
  17.     printf("****--Left Pyramid---****\n");
  18.     for(int i=0; i<rows; i++)
  19.     {
  20.         for(int l=0;l<rows-1-i;l++)
  21.         {
  22.             printf("  ");
  23.         }
  24.         for(int j=0; j<=i;j++)
  25.         {
  26.             printf("* ");
  27.         }
  28.         printf("\n");
  29.     }
  30.     printf("\n");
  31.     return 0;
  32. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. In the left aligned-pyramid pattern, the spaces are printed first, followed by the characters.
3. The number of spaces printed is equal to the total number of rows – line number.
4. After printing the spaces, we print the pattern’s character. It can be a *, an alphabet, or a number.
5. To print the characters, you must run a loop that iterates as many times as the line number.
6. For example, in the first line, we must print one character, in the second line, two characters, and in the third line, three characters, and so on until we reach the last line.

Time Complexity: O(n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity of left aligned pyramid is O(n2). (where n is the number of rows).

Space Complexity: O(1)
It uses constant space, so the space complexity of left aligned pyramid is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “7” to print the left aligned pyramid pattern.

Enter the number of rows: 7
****--Left Pyramid---****
            * 
          * * 
        * * * 
      * * * * 
    * * * * * 
  * * * * * * 
* * * * * * *

Method 7: Combine Two Half Pyramids

Combining 2 half pyramid will be a pattern such that we print a half pyramid and mirror it on the left side of the screen. It has a symmetry like a V letter in the alphabet.

Program/Source Code

Here is source code of the C Program to combine two half pyramids. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Combine Two Half Pyramids
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d",&rows);
  14.     printf("****--combine two half pyramids---****\n\n");
  15.     for(int i=1; i<=rows; i++)
  16.     {
  17.         printf("%d",i);
  18.         for(int j=0;j<i;j++)
  19.         {
  20.             printf("%c ",'*');
  21.         }
  22.         int spaces  = 2*(rows -i);
  23.         for(int j=0;j<spaces;j++)
  24.         {
  25.             printf("  ",spaces);
  26.         }
  27.         for(int j=0;j<i;j++)
  28.         {
  29.             printf("%c ",'*');
  30.         }
  31.         printf("\n");
  32.     }
  33.     printf("\n");
  34.     return 0;
  35. }
Program Explanation

1. For printing the pattern, take the number of rows as input and store in the variable rows.
2. The loop is then iterated through rows of times. This is going to be the outer loop.
3. There are three inner loops where the first loop outputs the first half of the pyramid. This loop iterates from 0 to line number
4. Then we need to print spaces, for which we will deduct the line number from the rows and multiply it by two.
5. Then we print the spacing that many times. The inverted half pyramid will be printed by the third inner loop.

Time Complexity: O(2n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity of combine two half pyramid is O(2n2). (where n = number of rows in the inverted pyramid).

Space Complexity: O(1)
It uses constant space, so the space complexity of combine two half pyramid is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “7” to print the combine two half pyramid pattern.

Enter the number of rows: 7
****--combine two half pyramids---****
 
1*                         * 
2* *                     * * 
3* * *                 * * * 
4* * * *             * * * * 
5* * * * *         * * * * * 
6* * * * * *     * * * * * * 
7* * * * * * * * * * * * * *

Method 8: Diamond Pattern

In this method, we can create a diamond pattern by joining the loops to print the full pyramid and inverted pyramid.

Program/Source Code

Here is source code of the C Program to print diamond pattern. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to print diamond pattern
  3.  */
  4.  
  5. include<stdio.h>
  6. #include<stdlib.h>
  7. int main()
  8. {
  9.     int rows;
  10.     int column;
  11.     int spaces;
  12.     printf("Enter the number of rows : ");
  13.     scanf(" %d",&rows);
  14.     //diamond pattern
  15.     printf("****--diamond pattern---****\n");
  16.     for(int i=0; i<rows; i++)
  17.     {
  18.         for(int l=0;l<rows-1-i;l++)
  19.         {
  20.             printf(" ");
  21.         }
  22.         for(int j=0; j<=i;j++)
  23.         {
  24.             printf("%c ",'*');
  25.         }
  26.         printf("\n");
  27.     }
  28.     for(int i=0; i<rows; i++)
  29.     {
  30.         for(int spaces=0;spaces<i;spaces++)
  31.         {
  32.             printf(" ");
  33.         }
  34.         for(int j=0;j<rows-i;j++)
  35.         {
  36.             printf("%c ",'*');
  37.         }
  38.         printf("\n");
  39.     }
  40.  
  41.     printf("\n");
  42.     return 0;
  43. }
Program Explanation

1. Take the number of rows as input and store in the variable rows.
2. Inside a for loop there are two for loops for spaces and asterisks respectively which are decreased and increased respectively as the loop progresses to print the initial increasing diamond pattern.
3. For the decreasing part, the spaces are increased while the asterisks are decreased with every progress of the loop.

Time Complexity: O(n2)
Here we are printing the number of (‘*’) for each respective row like n*(n-1)*(n-2)*……., so time complexity of diamond pattern is O(n2). (where n = number of rows in the inverted pyramid).

Space Complexity: O(1)
It uses constant space, so the space complexity of diamond pattern is O(1). Because we are not creating any arrays or other data structures that use space.

Run Time Testcases

In this case, we enter the number of rows as “10” to print the diamond pattern.

Enter the number of rows: 10
****--diamond pattern---****
         * 
        * * 
       * * * 
      * * * * 
     * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         *

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.