C Program to Print Star Pyramid Pattern

How to write a C Program to Print Star Pyramid Pattern with example?. And also show you how to print Pyramid Pattern (or Equilateral Triangle) with different symbols.

C Program to Print Star Pyramid Pattern using While Loop

This C program allows the user to enter the maximum number of rows he/she want to print as an Equilateral Triangle. Here, we are going to print the Pyramid of * symbols until it reaches the user-specified rows.

/* C Program to Print Star Pyramid Pattern */
#include <stdio.h>
 
int main() 
{
  int Rows, i, j, k = 0;
	
  printf("Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);
	
  printf("Printing Star Pyramid Pattern \n \n");
  for ( i = 1 ; i <= Rows; i++ ) 
    {
      for ( j = 1 ; j <= Rows-i; j++ ) 
      {
      	printf(" ");    	
      }
      while (k != (2 * i - 1))
      {
  	printf("*"); 
  	k++;
      }
      k = 0;
      printf("\n");
    }
  return 0;
}
C Program to Print Star Pyramid Pattern 1

Let us see the Nested for loop

Outer Loop – First Iteration

From the above C Programming screenshot, you can observe that the value of i is 1 and Rows is 10 so, the condition (i <= 10) is True. So, it will enter into second for loop

Inner For Loop – First Iteration
The j value is 1 and the condition (j <= 9) is True. So, it will start executing the statements inside the loop. So, it will start executing printf(” “) statement until the condition fails.

Inner While Loop – First Iteration

The k value is 0, and the condition k != 2*i – 1 (0 != 1) is True. So, it will start executing the statements inside the loop. So, it will start executing printf(“*”) statements until the condition fails.

Next, iteration will start from the beginning until both the Inner Loops and Outer loop conditions fail.

C Program to Print Star Pyramid Pattern using For Loop

In this C program, we just replaced the While Loop with the For Loop. I suggest you refer For Loop article to understand the logic.

/* C Program to Print Star Pyramid Pattern */
#include <stdio.h>
 
int main() 
{
  int Rows, i, j, k;
	
  printf("Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);
	
  printf("Printing Star Pyramid Pattern \n \n");
  for ( i = 1 ; i <= Rows; i++ ) 
    {
      for ( j = 1 ; j <= Rows-i; j++ ) 
      {
      	printf(" ");    	
      }
      for (k = 1; k <= (2 * i - 1); k++)
      {
   	printf("*"); 
      }
      printf("\n");
    }
  return 0;
}
Please Enter the Number of Rows:  12
Printing Star Pyramid Pattern 
 
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************

C Program to display Star Pyramid Pattern

This C program to print star pyramid allows the user to enter the Symbol and number of rows he/she want to print. It means, instead of printing pre-defined stars, it allows the user to enter their own character.

/* C Program to Print Pyramid Pattern */
#include <stdio.h>
 
int main() 
{
  int Rows, i, j, k = 0;
  char ch;
  
  printf("Please Enter any Symbol :  ");
  scanf("%c", &ch);  
	
  printf("Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);
	
  printf("Printing Pyramid Pattern \n \n");
  for ( i = 1 ; i <= Rows; i++ ) 
    {
      for ( j = 1 ; j <= Rows-i; j++ ) 
      {
      	printf(" ");    	
	  }
	  while (k != (2 * i - 1))
	  {
	  	printf("%c", ch); 
	  	k++;
	  }
	  k = 0;
      printf("\n");
    }
  return 0;
}
Please Enter any Symbol :  $
Please Enter the Number of Rows:  12
Printing Pyramid Pattern 
 
           $
          $$$
         $$$$$
        $$$$$$$
       $$$$$$$$$
      $$$$$$$$$$$
     $$$$$$$$$$$$$
    $$$$$$$$$$$$$$$
   $$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$$$
 $$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$