In this tutorial, we will discuss the C program to display pyramid star pattern
In this topic, we will learn how to create Pyramid star pattern using nested for loop in C language
Star Pyramid pattern 1
Star pyramid pattern
Program 1
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows,i,j,k,l;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//outer for loop
for(j=1; j<=(rows-i)*2; j++){
printf(" ");//print space fot pyramid
}
for(k=i; k>=1; k--){//inner for loop
printf(" *"); //create left half
}
for(l=2; l<=i; l++){
printf(" *"); //create right half
}
printf("\n");
}
getch();
return0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows,i,j,k,l;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//outer for loop
for(j=1; j<=(rows-i)*2; j++){
printf(" ");//print space fot pyramid
}
for(k=i; k>=1; k--){//inner for loop
printf(" *"); //create left half
}
for(l=2; l<=i; l++){
printf(" *"); //create right half
}
printf("\n");
}
getch();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows,i,j,k,l;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//outer for loop
for(j=1; j<=(rows-i)*2; j++){
printf(" ");//print space fot pyramid
}
for(k=i; k>=1; k--){//inner for loop
printf(" *"); //create left half
}
for(l=2; l<=i; l++){
printf(" *"); //create right half
}
printf("\n");
}
getch();
return 0;
}
Star pyramid pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number of rows
6
*
***
*****
*******
*********
***********
Enter the number of rows
6
*
***
*****
*******
*********
***********
Enter the number of rows
6
*
***
*****
*******
*********
***********
Star Pyramid pattern 2
Inverted star Pyramid
Program 2
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=rows; i>=1; i--){//parent for loop to rows
for(j=i; j<=rows; j++){
printf(" ");//print space
}
for(j=1; j<=(2*i-1); j++){//inner for loop
printf("*");//print star
}
printf("\n");//move to next line
}
getch();
return0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=rows; i>=1; i--){//parent for loop to rows
for(j=i; j<=rows; j++){
printf(" ");//print space
}
for(j=1; j<=(2*i-1); j++){//inner for loop
printf("*");//print star
}
printf("\n");//move to next line
}
getch();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=rows; i>=1; i--){//parent for loop to rows
for(j=i; j<=rows; j++){
printf(" ");//print space
}
for(j=1; j<=(2*i-1); j++){//inner for loop
printf("*");//print star
}
printf("\n");//move to next line
}
getch();
return 0;
}
Inverted star pyramid pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number of rows
6
***********
*********
*******
*****
***
*
Enter the number of rows
6
***********
*********
*******
*****
***
*
Enter the number of rows
6
***********
*********
*******
*****
***
*
Star Pyramid pattern 3
Right leaned star pyramid pattern
Program 3
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//parent for loop to rows
for(j=1; j<=i; j++){
printf("*");// //print star to top half
}
printf("\n");//move to next line
}
for(i=rows; i>=1; i--){//outer for loop
for(j=1; j<=i; j++){//inner for loop
printf("*");//print star to bottom half
}
printf("\n");//move to next line
}
getch();
return0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//parent for loop to rows
for(j=1; j<=i; j++){
printf("*");// //print star to top half
}
printf("\n");//move to next line
}
for(i=rows; i>=1; i--){//outer for loop
for(j=1; j<=i; j++){//inner for loop
printf("*");//print star to bottom half
}
printf("\n");//move to next line
}
getch();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//parent for loop to rows
for(j=1; j<=i; j++){
printf("*");// //print star to top half
}
printf("\n");//move to next line
}
for(i=rows; i>=1; i--){//outer for loop
for(j=1; j<=i; j++){//inner for loop
printf("*");//print star to bottom half
}
printf("\n");//move to next line
}
getch();
return 0;
}
Right leaned pyramid pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the numbers of rows
6
*
**
***
****
*****
******
*****
****
***
**
*
Enter the numbers of rows
6
*
**
***
****
*****
******
*****
****
***
**
*
Enter the numbers of rows
6
*
**
***
****
*****
******
*****
****
***
**
*
Star Pyramid pattern 4
Left leaned pyramid triangle
Program 4
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//for loop to rows
for(j=i; j<rows; j++){
printf(" ");// //print space
}
for(j=1; j<=i; j++){//inner for loop
printf("*");//print star to top half
}
printf("\n");//move to next line
}
for(i=rows; i>=1; i--){//for loop to rows
for(j=i; j<=rows; j++){
printf(" ");// //print space
}
for(j=1; j<i; j++){//inner for loop
printf("*");//print star to bottom half
}
printf("\n");//move to next line
}
getch();
return0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//for loop to rows
for(j=i; j<rows; j++){
printf(" ");// //print space
}
for(j=1; j<=i; j++){//inner for loop
printf("*");//print star to top half
}
printf("\n");//move to next line
}
for(i=rows; i>=1; i--){//for loop to rows
for(j=i; j<=rows; j++){
printf(" ");// //print space
}
for(j=1; j<i; j++){//inner for loop
printf("*");//print star to bottom half
}
printf("\n");//move to next line
}
getch();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,rows;
printf("Enter the number of rows\n");
scanf("%d",&rows);
for(i=1; i<=rows; i++){//for loop to rows
for(j=i; j<rows; j++){
printf(" ");// //print space
}
for(j=1; j<=i; j++){//inner for loop
printf("*");//print star to top half
}
printf("\n");//move to next line
}
for(i=rows; i>=1; i--){//for loop to rows
for(j=i; j<=rows; j++){
printf(" ");// //print space
}
for(j=1; j<i; j++){//inner for loop
printf("*");//print star to bottom half
}
printf("\n");//move to next line
}
getch();
return 0;
}
Left leaned pyramid pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number of rows
6
*
**
***
****
*****
******
*****
****
***
**
*
Enter the number of rows
6
*
**
***
****
*****
******
*****
****
***
**
*
Enter the number of rows
6
*
**
***
****
*****
******
*****
****
***
**
*