Star Pattern Rectangle Shape Print in C-Language

For any kind of star pattern we can use one common structure code for different patterns with slight modifications.

Example1: To print single star?

void main()

{

printf("*");

}

output: *

But if we want to print 2 or more “*” symbols like in a row “*****”?

Example2:

void main()

{

printf("*****");

}

output: *****

like this we can print 5 stars in row but there is no programming  generalization.

suppose if we want to print 20 or 30 stars , the above process (example2) will be fail. Nobody ready to write n num of stars in program.

We need to generalize the program by using simple logic need to reduce the complexity.

Example3:

in example 1 we print one “*”, Now by using “Loop” we need to do repetitive process into 5 times.

main()

{

int  j;

for(j=1;j<=5;j++)

{

printf("*");

}

printf("\n");

}

output: *****

like this by using for loop we can generate n number of “*”  symbols.

in the same procedure if we want to print 5- stars in multiple rows and columns

*****

*****

*****

like in the above picture , 3 rows and 5 columns.

the same logic we are using for generate this pattern.

in the above process we apply reparative  5 times  for single star.

similarly , here need to do repetitions for “*****”

#include<stdio.h>

1-void main()

2-{

3-int i,j;

4-for(i=1;i<=5;i++)

5-{

6-for(j=1;j<=5;j++)

7-{

8-printf(“*”);

9-printf(” “); (6 to 10) output:*****

10-}

11-printf(“\n”);

12-}

13-getch();

14-}

here we use 2 int variables i and j

                j1            j2            j3            j4            j5

i1             *             *             *             *             *

i2             *             *             *             *             *

i3             *             *             *             *             *

i4             *             *             *             *             *

i5             *             *             *             *             *

I – denotes rows and j- denotes columns. in for loop by changing values of i and j we can create

any number of rows and columns like this.

—————————————————————————————————————————————–

user not interested to change values every time in program for print number of rows and columns,

then dynamically can enter the value at the time of execution like this , by using one additional variable

“n”.

Leave a Reply

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