Ad Code

Responsive Advertisement

C Stars pattern codes_____How To Do Stars Decoration by using C___

 



How To Do Stars Decoration by using C


In this tutorial, we learn how to do the decoration of stars by using C. For this, we'll use different methods which are the following read carefully.

  

  • By simple Way
  • By using loops 
  • By using Functions

LET START

By Simple Way:

         By using simple Way, we'll use printf() command only. Let me show you how to do this :

#include<stdio.h>
main()
{
    printf("\t\n*\n**\n***\n****\n*****\n******\n*******\n" );

    printf("\t\n*****\n*****\n*****\n*****\n*****\n");
}


 

____________________________________________ 


By using Loops:


In order to decorate stars in C-language, We use Three different loops which are While, Do-While, and For loop. Let's start and study them one by one. 


Using While Loop

#include<stdio.h>

main()

{

   int a,b;

   a=1;

   while(a<=10)

   {

     b=a;

     while(b<=10)

     {

         printf("*");

        b++;

     }

        printf("*\n");

       a++;

   }

}


stars using while loop


                                 ____________________________________________

Using Do-While Loop 


#include<stdio.h>

main()

{

   int a,b;

   a=1;

   do

   {

       b=a;

       while(b<=10)

       {

           printf("*");

           b++;

       }

     printf("\n");

     a++;

   }

   while(a<=10);

}

 

stars using Do-While

___________________________________________________ 


For Loop 

Program#1: 

#include<stdio.h>

main()

{

 int a,b;

 for(a=1;a<=10;a++)

 {

     for(b=1;b<=10;b++)

        printf("*");

    printf("\n");

 }

}

stars using For loop method#1

 

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

Program#2:

#include<stdio.h>

main()

{

 int a,b,c;

 for(a=1;a<=10;a++)

 {

     for(b=1;b<=10;b++)

        printf(" ");

     for(c=1;c<=a;c++)

        printf("*");

        printf("*");

    printf("\n");

 }

}

stars using for loop  method#2 

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


Program#3:

#include<stdio.h>

main()

{

 int a,b,c;

 for(a=1;a<=10;a++)

 {

     for(b=1;b<=10-a;b++)

        printf(" ");

     for(c=1;c<=a;c++)

        printf("*");

    printf("\n");

 }

}

stars using For loop method#3

___________________________________________


Using While and For Together

#include<stdio.h>

main()

{

 int a,b;

 for(a=1;a<=10;a++)

 {

     b=a;

     while(b<=10)

     {

         printf("*");

         b++;

     }

   printf("\n");

 }

}

stars using For & while loop
 

__________________________________________ 


By using Functions:

#include<stdio.h>

void star(void);

void star(void)

{

    int a,b;

    for(a=1;a<=10;a++)

    {

        b=a;

        while(b<=10)

        {

            printf("*");

            b++;

        }

        printf("\n");

    }

void main()

    {

        star();

    }

}

___________________________________________________________ 



Reactions

Post a Comment

2 Comments