C Language

Program to pyramid star pattern in C – using loops

Program to pyramid star pattern  in C- using loops

In this article, we will discuss the  program to pyramid star pattern in c- using loops

In this post, we will learn how to create the Pyramid star pattern using for, while and do-wile loop

pyramid pattern – using for loop

Pattern 1

This program allows the user to enter the number of rows and the symbol then the program displays a full pyramid star pattern with a given symbol using for loop in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,k,rows;
    char ch;
    printf("Enter the number of rows for pyramid pattern: ");
    scanf("%d%c",&rows,&ch);
    printf("Enter the symbol as you wish: ");
    ch=getchar();

   for(i=1; i<=rows; i++){//iterates trough rows in pyramid
   for(j=0; j<rows-i; j++){//innr for loop 1
     printf(" ");//print space

}
for(k=1; k<2*i; k++){//inner for loop 2
     if(i==rows || (k==1 || k == 2*i-1)){
   printf("%c",ch);//print input character when if condition returns rue
   }
   else{
  printf("%c",ch);//otherwise print input character
}
}
printf("\n");//move to next line
   }
   getch();
    return 0;
}

When the above code is executed it produces the following result

Output 1

Output 1

Output 2

Output 2

pyramid pattern- using the while loop

Pattern 2

This program allows the user to enter the number of rows and the symbol then the program displays a full pyramid star pattern with a given symbol using while loop in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,k,rows;
    char ch;
    printf("Enter the number of rows for pyramid pattern: ");
    scanf("%d%c",&rows,&ch);
    printf("Enter the symbol as you wish: ");
    ch=getchar();
i=1;
   while(i<=rows){//iterates trough rows in pyramid
            j=0;
   while(j<rows-i){//inner while loop 1
     printf(" ");//print space
 j++;
}
k=1;
while(k<2*i){//inner while loop 2
     if(i==rows || (k==1 || k == 2*i-1)){
   printf("%c",ch);//print input character when if condition returns rue
   }
   else{
  printf("%c",ch);//otherwise print input character
}
 k++;
}
printf("\n");//move to next line
 i++;
   }
   getch();
    return 0;
}

When the above code is executed it produces the following result

Output 1

Output 3

Output 2

Output 4

 

pyramid pattern – using the do-while loop

pattern 3

This program allows the user to enter the number of rows and the symbol then the coding displays a full pyramid star pattern with a given symbol using the do-while loop in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,k,rows;
    char ch;
    printf("Enter the number of rows for pyramid pattern: ");
    scanf("%d%c",&rows,&ch);
    printf("Enter the symbol as you wish: ");
    ch=getchar();
i=1;
   do{//iterates trough rows in pyramid
            j=0;
  do{//inner while loop 1
     printf(" ");//print space
 j++;
} while(j<=rows-i);
k=1;
do{//inner while loop 2
     if(i==rows || (k==1 || k == 2*i-1)){
   printf("%c",ch);//print input character when if condition returns rue
   }
   else{
  printf("%c",ch);//otherwise print input character
}
 k++;
}while(k<2*i);
printf("\n");//move to next line
 i++;
   }while(i<=rows);
   getch();
    return 0;
}

When the above code is executed it produces the following result

Output 1

Output 5

 

Output 2

Pattern 6

 

Suggested post you

For loop in C language

While loop in C language

Do-while loop in C language

Nested for loop in C language

Nested while loop in C language

 

Similar post

C++ pyramid star pattern  – using loops

Java pyramid star pattern – using loops

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.