Star Pattern code in C programing with solutions

 

In this tutorial, we will learn how to do Star Pattern code in C programing language. All these codes are build using the control statement.

Star Pattern code in c

The star patterns contain different types of code. Like some of them are given below.

Pattern 1

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

Pattern 2

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

Pattern 3

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

Pattern 4

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

Pattern 5

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

A solution of Star pattern 1

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

int main()
{
    int row, col;       //Variable declaration
    
    for(row=0;row<5;row++)      //Parent loop
    {
        for(col=0;col<5;col++)      //Body Loop
        {                   
            printf("*");    //printing statement
        }       
        printf("\n");       //for next line after printing a row
    }       

    return 0;
}

Output: 

star pattern 1 in c language

An explanation for this pattern code :

  • Firstly, we declare two variables of an integer data type.
  • Using the parent loop we increment the value of rows after then print the columns using body loop.
  • printf statement is used for printing the star in this code.
  • After printing the first-row the cursor moves to the next line by printing escape sequence.

Pattern 2 solutions Half opposite pyramid in c language

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

int main()
{
    int row, col;       //Variable declare
    
    for(row=5;row>0;row--)      //Parent loop
    {
        for(col=row;col>0;col--)      //Body Loop
        {                   
            printf("*");    //printing statement
        }       
        printf("\n");       //for next line after printing a row
    }       

    return 0;
}

Output: 

star pattern 2 in c language

Half pyramid star pattern in c language

 

 

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

int main()
{
    int row, col;       //Variable declaration
    
    for(row=0;row<5;row++)      //Parent loop
    {
        for(col=0;col<=row;col++)      //Body Loop
        {                   
            printf("*");    //printing statement
        }       
        printf("\n");       //for next line after printing a row
    }       

    return 0;
}

Output:

pattern 3 half pyramid star pattern in c language.jpg

Pattern 4: Half pyramid code in c programming

For building this kind of pattern. Analyze the pattern carefully. After that print some space and then the print pattern in increasing order and space in decreasing order.

for example: 

row/col01234
0spacespacespacespace*
1spacespacespace**
2spacespace***
3space****
4*****
#include <stdio.h>
#include <conio.h>

int main()
{
    int row, col;  //Variable declartion
    int size=5;
    
    for(row=0;row<size;row++)      //Parent loop
    {
        for(col=size;col>row;col--)      //Body Loop
        {                   
            printf(" ");    //print some space
        }  
        for(col=0;col<=row;col++)      //Body Loop
        {                   
            printf("*");    //printing statement
        }
        printf("\n");       //for next line after printing a row
    }       

    return 0;
}

Output: 

pattern 4 half pyramid with space star pattern in c language

Pattern 5: C program of half pyramid with space

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

int main()
{
    int row, col;  //Variable declartion
    int size=5;
    
    for(row=0;row<size;row++)      //Parent loop
    {
        for(col=0;col<row;col++)      //Body Loop
        {                   
            printf(" ");    //printing statement
        }  
        for(col=0;col<size-row;col++)      //Body Loop
        {                   
            printf("*");    //printing statement
        }
        printf("\n");       //for next line after printing a row
    }       

    return 0;
}

 

Output: 

pattern 5 inverted half pyramid with space star pattern in c language

 

Leave a Reply

Your email address will not be published. Required fields are marked *