This article will help you create various pyramid pattern in C language or you can say star pattern program in C and will also help you explain the code, with online working link sample.

Before we proceed to code, let me explain the basics of pyramid pattern printing in any language is that, we use basic For loop and If/else in it.

Now there can be two-three for loops in each pattern(depends on requirement basically), suppose if there are two loops(used in many patterns), outer loop is basically to print or create rows of the triangle, while inner loop is used to fill the rows with data(it can be * or numbers like 1,2,3....).

pyramid-pattern-in-c.png

1. Program to print right angle pyramid using *

So in this pattern, we will declare three int variables first, 2 variables are used for loops while 1 variable is to get a number of printing rows from the user, I have explained each line of code with comments, have a look.

#include <stdio.h>
int main()
{
    //declare variables to use
    int i, j, rows;
    
    //Enter the number of rows you want to print, printf shows this command to user
    printf("Enter number of rows: ");
  
    //scanf is used to get value, and save that value in 'rows' variable
    scanf("%d",&rows);

    //now loop for each row
    for(i=1; i<=rows; ++i)
    {
        //this loop will print * until value of outer loop variable(i) is greater than 
        //inner loop vairable(j)
        for(j=1; j<=i; ++j)
        {
            printf("* ");
        }
        //as soon as inner loop(j) value becomes greater than outer loop printing * ends
       // & it prints new line using \n
        printf("\n");
    }
    return 0;
}

Executing the above code will give you following output

Enter number of rows: 6                                        
*  
* *
* * * 
* * * * 
* * * * * 
* * * * * *                        

Here the working link for the online demo https://www.onlinegdb.com/ry41pFlSG

2. Printing Full PyramidTriangle using *

This program is similar to above one, but little bit different, now we would need to declare four variables instead of three this time.

As you can understand to print triangle, we would first need to print a space(" ") for number of time then asterisk(*) then new line (\n), and repeat the procedure, let's check a code and understand it with comments

#include <stdio.h>
 
int main()
{
    //declare variables
   int row, c, n, temp;
 
   //ask user to enter number of rows to print
   printf("Enter the number of rows to print pyramid rows ");
   //get number of rows value in variable "n"
   scanf("%d",&n);
  
  //set this value in temp because this is help us print blank space before printing *
  // now we need temp as we need to keep value of "n" same through out the program
  //but "temp" value will be decreased after each loop
   temp = n;
 
 //let's start with outer loop to loop through rows which will be dependent on variable "n"
   for ( row = 1 ; row <= n ; row++ )
   {
       //here C start with 1 and will keep printing space until it's value is greater current "temp" value
      for ( c = 1 ; c < temp ; c++ )
         printf(" "); // space
         
      //decrease temp value to print one less space next time we loop
      temp--;
      
      //now loop to print *, check formular here it's 2N-1 this is because
      // we need to print * odd number of times to get our pattern out
      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");
 
      //as soon as loop ends print a new line
      printf("\n");
   }
 
   return 0;
}

Output of the above code

Enter the number of rows to print pyramid rows 5                             
    *                                                                          
   ***                                        
  *****                                                            
 *******                                                  
********* 

as you can see in the above code the inner loop has the formula (2N-1), as to print triangle we need to print * for 1 time, then 3 times, then 5 times & so on.

Here is the working link https://onlinegdb.com/HkOfd5erG

3. Inverted Full Triangle pattern

In this pattern, which will be inverted pattern of above example.

Suppose we have n rows. First row will contain 0 space followed by 2*n-1 stars, 2nd row will contain 1 space followed by 2*n-3 stars, 3rd row will contain 2 spaces followed by 2*n-5 stars and so on.. We can use nested loops to print this pattern where outer loop represents row number (say i) and inner loop prints the space (i-1 times) followed by the star pattern (2*n-(2*i-1) times).

Basically, if you will check and understand the below code, we have reversed the code, means now for row 1, we are printing all stars, then for 2nd row, print space for one time and then *, and break line, now for 3rd row, two times space, star for 2n-1 time, break the line & so on.

#include <stdio.h>

int main(void)
{
	// n is number of rows
	int n = 5;

	int i, j, k;

	// loop for each row
	for (i = n; i >= 1; i--)
	{
		// print space
		for (j = n; j > i; j--)
			printf(" ");

		// print '*'
		for (k = 1; k < (i * 2); k++)
			printf("*");
		
		// move to the next line
		printf("\n");
	}
		
	return 0;
}

Output of above code

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

Working link here https://onlinegdb.com/SyZ0JsgSM

4. Printing Pyramid using Numbers

Suppose we need to print below triangle using C

        1
       232
      34543
     4567654
    567898765

The following will be code for it

#include <stdio.h>
int main()
{
    int i, space, rows, k=0, count = 0, count1 = 0;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
       //print space until sapce value is not equal/less than (sapce-i), where
        // i is current row Number
        for(space=1; space <= rows-i; ++space)
        {
            printf("  ");
            ++count; //increment count after each space
        }

        //after printing all spaces, let's start number printing loop
        // here while loop is used and it will print unless 
        //k is not euqal to 2*CurrentRow-1
        while(k != 2*i-1)
        {
           //Now, looking at pattern formula is to print I(CurrentRow)+ Current K
           // if Count is less than TotalRow -1
            if (count <= rows-1)
            {
                printf("%d ", i+k);
                ++count;
            }
            
            else
            {
                ++count1;
                printf("%d ", (i+k-2*count1));
            }
            ++k;
        }
        //reset all values to 0,except CurrentRow value and total row value
        count1 = count = k = 0;

        printf("\n");
    }
    return 0;
}

I have explained the code in comments, here is the working sample https://onlinegdb.com/ByecSogHG

5. Print Diamond pattern with star in C

Now in this section, we will create diamond of star pattern in C using for loop, as shown below

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

Here is the code for it.

#include<stdio.h>
#include<conio.h>

void main() {
   int i, j, k;
   //first loop for upper half of triangle
   for (i = 1; i <= 5; i++) {
       //loop for adding space before printing star
      for (j = i; j < 5; j++) {
         printf(" ");
      }
      //loop for printing "*"  star
      for (k = 1; k < (i * 2); k++) {
         printf("*");
      }
      printf("\n");
   }
   
   //second for loop lower half of triangle
   for (i = 4; i >= 1; i--) {
        //loop for adding space before printing star
      for (j = 5; j > i; j--) {
         printf(" ");
      }
       //loop for printing "*"  star
      for (k = 1; k < (i * 2); k++) {
         printf("*");
      }
      printf("\n");
   }
   getch();
}

In the above C code, we are first printing star pattern of upper half and then lower half. I have commented out few lines of code, to understand the logic.

You may also like to read:

Program for Matrix multiplication in C (With & Without pointers)

Sorting array elements in ascending order program in C

Fibonacci Series program in C ( With and Without recursion)

Simple C programming examples with output (must read for beginners)

Introduction to C++ File handling

Switch case in C++ with example