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

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.

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

- Full Pyramid Pattern in C
- Full Pyramid with Alphabets in C
- Half Pyramid Pattern in C
- Inverted Pyramid Pattern in C
- Inverted Half Pyramid Pattern in C
- Left Aligned Pyramid Pattern in C
- Combine Two Half Pyramids in C
- Diamond Pattern in C

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

* * * * * * * * * * * * * * * * * * * * *

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.

`/*`

`* C Program to Print Full Pyramid`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d", &rows);

`//full pyramid`

printf("****--Full Pyramid---****\n");

for(int i=0; i<rows; i++)

`{`

for(int l=0;l<rows-1-i;l++)

`{`

printf(" ");

`}`

for(int j=0; j<=i;j++)

`{`

printf("%c ",'*');

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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.

**Time Complexity: O(n ^{2})**

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

^{2}). (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.

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

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

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.

`/*`

`* C Program to Print Full Pyramid with alphabets`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d",&rows);

printf("****--full pyramid---****\n");

for(int i=0; i<rows; i++)

`{`

`// loop to print the row-line number of spaces`

for(spaces=0;spaces<rows-1-i;spaces++)

`{`

printf(" ");

`}`

`//loop to print the characters`

for(int j=0; j<=i;j++)

`{`

printf("%c ",'a'+j);

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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(n ^{2})**

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

^{2}). (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.

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

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.

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.

`/*`

`* C Program to Print Half Pyramid`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d",&rows);

printf("\n");

printf("****--half pyramid---****\n");

for(int i=0; i<rows; i++)

`{`

for(int j=0; j<=i;j++)

`{`

printf("* ");

`}`

printf("\n");

`}`

return 0;

`}`

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(n ^{2})**

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(n

^{2}). (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.

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

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.

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.

`/*`

`* C Program to Print Inverted Pyramid`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d",&rows);

printf("****--Inverted Pyramid---****\n\n");

for(int i=0; i<rows; i++)

`{`

for(int spaces=0;spaces<i; spaces++)

`{`

printf(" ");

`}`

for(int j=0;j<rows-i;j++)

`{`

printf("* ");

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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(n ^{2})**

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(n

^{2}). (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.

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

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.

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.

`/*`

`* C Program to Print Inverted Half Pyramid`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows: ");

scanf("%d",&rows);

printf("****--Half Inverted Pyramid---****\n\n");

for(int i=0; i<rows; i++)

`{`

for(int spaces =0;spaces<i;spaces++)

`{`

printf(" ");

`}`

for(int j=0;j<rows-i;j++)

`{`

printf("* ");

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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(2n ^{2})**

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(2n

^{2}). (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.

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

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.

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.

`/*`

`* C Program to Print Left Aligned Pyramid`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d",&rows);

printf("\n");

`//left pyramid....`

printf("****--Left Pyramid---****\n");

for(int i=0; i<rows; i++)

`{`

for(int l=0;l<rows-1-i;l++)

`{`

printf(" ");

`}`

for(int j=0; j<=i;j++)

`{`

printf("* ");

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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(n ^{2})**

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(n

^{2}). (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.

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

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.

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.

`/*`

`* C Program to Combine Two Half Pyramids`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d",&rows);

printf("****--combine two half pyramids---****\n\n");

for(int i=1; i<=rows; i++)

`{`

printf("%d",i);

for(int j=0;j<i;j++)

`{`

printf("%c ",'*');

`}`

int spaces = 2*(rows -i);

for(int j=0;j<spaces;j++)

`{`

printf(" ",spaces);

`}`

for(int j=0;j<i;j++)

`{`

printf("%c ",'*');

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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(2n ^{2})**

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(2n

^{2}). (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.

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

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

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.

`/*`

`* C Program to print diamond pattern`

`*/`

include<stdio.h>

`#include<stdlib.h>`

int main()

`{`

int rows;

int column;

int spaces;

printf("Enter the number of rows : ");

scanf(" %d",&rows);

`//diamond pattern`

printf("****--diamond pattern---****\n");

for(int i=0; i<rows; i++)

`{`

for(int l=0;l<rows-1-i;l++)

`{`

printf(" ");

`}`

for(int j=0; j<=i;j++)

`{`

printf("%c ",'*');

`}`

printf("\n");

`}`

for(int i=0; i<rows; i++)

`{`

for(int spaces=0;spaces<i;spaces++)

`{`

printf(" ");

`}`

for(int j=0;j<rows-i;j++)

`{`

printf("%c ",'*');

`}`

printf("\n");

`}`

printf("\n");

return 0;

`}`

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(n ^{2})**

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(n

^{2}). (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.

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

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Apply for C Internship
- Practice BCA MCQs
- Buy Computer Science Books