Friday, 3 March 2017

check how to print patterns using programming language.



Making patterns on computer using programming language is quiet common. But some of us find it difficult to start first and sometimes even after trying we don’t get the actual thing.
Here are some patterns with their explanation given using c programming language.


1.   Pattern first:-
*
*
*
*      (4*1)

We have to print this pattern on the computer screen using c programming language.
Before starting writing code first we have to analyze what we actually have to do.
In this pattern we can clearly see that there are four rows and one column. That means we have to print one star in each row.
Now writing the code part:-

#include<stdio.h>
int main()
{
            int row,column;
            for(row=1;row<=4;row++)
            {
                        printf("*"); //star printing
                        printf("\n");            //line changing
            }
return 0;      
}

Here only row variable is used to print and change line.

     2.   Pattern second:-

        * * *
        * * *        (2*3)

In this pattern we can clearly see that there are two rows and three columns. That means we have to print three stars in each row.

Now writing the code part:-

#include<stdio.h>
int main()
{
            int row,column;
            for(row=1;row<3;row++)
            {
                        for(column=1;column<=3;column++)
            {         
                        printf("*"); //star printing second loop
            }
                        printf("\n");            //line changing first loop
            }
return 0;      
}

    3.   Pattern third:-

        ****
        ***
        **
        *
In this pattern we can clearly see that there are four rows and four columns. Clearly one star is less than the previous row. Row variable is used to control the change in row and column variable is responsible for printing.

Now writing the code part:-

#include<stdio.h>
int main()
{
            int row,column;
            for(row=1;row<=5;row++)
            {
                        for(column=1;column<=5-row;column++)
            {         
                        printf("*"); //star printing second loop
            }
                        printf("\n");            //line changing first loop
            }
return 0;      
}

     4.   Pattern four:-
*
* *
* * *
* * * *

In this pattern we can clearly see that there are four rows and four columns. But with change in row one star adds to the pattern. Row variable is used to control the change in row and column variable is responsible for printing. This pattern is opposite of the third pattern.

Now writing the code part:-

#include<stdio.h>
int main()
{
            int row,column;
            for(row=1;row<5;row++)
            {
                        for(column=1;column<=row;column++)
            {         
                        printf("*"); //star printing second loop
            }
                        printf("\n");            //line changing first loop
            }
return 0;      
}

    5.   Pattern five:-

   *
* *
     * * *
  * * * *

In this pattern we can clearly see that there are four rows and four columns. But with change in row one star adds to the pattern. Row variable is used to control the change in row and column variable is responsible for printing.
In this space variable is used to print spaces.

Now writing the code part:-

#include<stdio.h>
int main()
{
            int row,column,space;
            for(row=1;row<5;row++)
            {
                        for(space=0;space<4-row;space++)
                        {
                                    printf(" ");   //printing space
                        }
                        for(column=1;column<=row;column++)
            {         
                        printf("*"); //star printing second loop
            }
                        printf("\n");            //line changing first loop
            }
return 0;      
}