Star pattern in C – free C programming tutorial

In this you will be familiarize with the various star pattern, which we can draw by using C programming language with the help of iteration statements and conditional statements.

So let us discuss some sample star patterns along with their code in C language.


Star pattern :

Star Pattern 1.

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

#include <stdio.h>   // Header file

int main()   // main function
{
// declaration of local variables
    int i, j;

// Use of nested for loop which in a iteration statement
    for(i=1;i<=5;i++) // for rows
    {
        for(j=1;j<=i;j++)  // for columns
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Above code uses nested-for iteration statement.
Variable i and j are counter variable and value of these two variables specifies rows and column.
Variable i is initialised with 0 and is incremented by one every time when the loop executes on the condition that  i should be less than or equal to 5.
After satisfying this condition, inner loop executes in which variable j is initialised with 1 and is incremented by one every time when the loop executes on the condition that j should be less than or equal to i.

Result of above code after compilation and execution is given as the following :
OUTPUT :
*
**
***
****
*****

Star Pattern – 2

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

#include <stdio.h>

int main()
{
    int i, j;

    for(i=5;i>=1;i--)  // for rows
    { 
        for(j=1;j<=i;j++)  // for column
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Above code uses nested-for iteration statement.
Variable i and j are counter variable and value of these two variables specifies rows and column.
Variable i is initialised with 5 and is decremented by one every time when the loop executes on the condition that  i should be greater than or equal to 1.
After satisfying this condition, inner loop executes in which variable j is initialised with 1 and is incremented by one every time when the loop executes on the condition that j should be less than or equal to i.
Result of above code after compilation and execution is given as the following :
OUTPUT :
*****
****
*** 
**
*

Star Pattern – 3

#include <stdio.h>

int main()
{
    int i, j, k;
    for(i=5;i>=1;i--)  // for rows
    {
        for(j=1;j<i;j++)  // for columns
        {
            printf(" ");
        }
        for(k=5;k>=i;k--)  // for spacing
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Above code uses nested-for iteration statement.
Variable i, j and k are counter variable and value of these variables specifies rows,column and spacing respectively.
Variable i is initialised with 5 and is decremented by one every time when the loop executes on the condition that  i should be greater than or equal to 1.
After satisfying this condition, inner loop executes in which variable j is initialised with 1 and is incremented by one every time when the loop executes on the condition that j should be less than or equal to i.
Variable k is initialised with 5 and is decremented by one every time when the loop executes on the condition that k should be greater than or equal to 1.
Result of above code after compilation and execution is given as the following :
OUTPUT :
    *
   **
  ***
 ****
*****

Star Pattern – 4

#include <stdio.h>

int main()
{
    int i, j, k;
    for(i=5;i>=1;i--)  // for rows
    {
        for(j=5;j>i;j--) // for columns
        {
            printf(" ");
        }
        for(k=1;k<=i;k++) // for spacing
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Above code uses nested-for iteration statement.
Variable i, j and k are counter variable and value of these variables specifies rows,column and spacing respectively.
Variable i is initialised with 5 and is decremented by one every time when the loop executes on the condition that  i should be greater than or equal to 1.
After satisfying this condition, inner loop executes in which variable j is initialised with 5 and is decremented by one every time when the loop executes on the condition that j should be greater than i.
Variable k is initialised with 1 and is incremented by one every time when the loop executes on the condition that k should be less than or equal to i.
Result of above code after compilation and execution is given as the following :
OUTPUT :
*****
 ****
  ***
   **
    *

Star Pattern – 5

#include <stdio.h>

int main()
{
    int i, j, k;
    for(i=1;i<=5;i++)  // for rows
    {
        for(j=i;j<5;j++)  // for columns
        {
            printf(" ");
        }
        for(k=1;k<(i*2);k++)   // for spacing
        {
                printf("*");
        }
        printf("\n");
    }

    return 0;
} 

Above code uses nested-for iteration statement.

Variables i, j and k are counter variable and value of these variables specifies rows,column and spacing respectively.
Variable i is initialised with 1 and is incremented by one every time when the loop executes on the condition that i should be less than or equal to 5.
After satisfying this condition, inner loop executes in which variable j is initialised with 1 and is incremented by one every time when the loop executes on the condition that j should be less than or equal to i.
Variable k is initialised with 1 and is incremented by one every time when the loop executes on the condition that k should be less than i multiply by 2.
Result of above code after compilation and execution is given as the following :
OUTPUT :
        *
       ***
      *****
     *******
    *********

Star Pattern – 6

#include <stdio.h>

int main()
{
    int i, j, k;
    for(i=5;i>=1;i--)  // for rows
    {
        for(j=5;j>i;j--)  // for columns
        {
                printf(" ");
        }
        for(k=1;k<(i*2);k++)   // for spacing
        {
                printf("*");
        }
        printf("\n");
    }

    return 0;
}
Variables i, j and k are counter variable and value of these variables specifies rows,column and spacing respectively.
Variable i is initialised with 5 and is decremented by one every time when the loop executes on the condition that i should be greater than or equal to 1.
After satisfying this condition, inner loop executes in which variable j is initialised with 5 and is decremented by one every time when the loop executes on the condition that j should be greater than i.
Variable k is initialised with 1 and is incremented by one every time when the loop executes on the condition that k should be less than i multiply by 2.
Result of above code after compilation and execution is given as the following :
OUTPUT :
  *********
   ******* 
    *****
     *** 
      *

If you have any doubt please comment below.

Leave a Comment

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