# C PROGRAMS : PATTERN

C PROGRAMS : PATTERN
6. Program to print the given pattern
// Program to print the given patter :

/*

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.

outer loop "i" runs from 5 to 1 since we have 5 rows.
We run the "i" loop in decreasing order ( i.e from 5 to 1 )
since rows is in increasing order(5 4 3 ... 1 diagnol value)

Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 5 to i.
We run the "j" loop in decreasing order ( i.e from 5 to i )
since column is in decreasing order ( 5 4 ... i single line value )

We print "value" in the inner loop
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;

/* Since we have five rows but now we should also keep an eye on the pattern to be printed.

Since the pattern to be printed is in descending order so we iterate the "i" loop from 5 to 1 ( i.e 5 times ) */

for(i=5;i>=1;i--)
{
/* Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 5 to i. */

for(j=5;j>=i;j--)
{
/* Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}

// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this
return 0;
}

7. Program to print the given pattern
```// Program to print the given patter :

/*

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.

outer loop "i" runs from 5 to 1 since we have 5 rows.
We run the "i" loop in decreasing order ( i.e from 5 to 1 )
since rows is in decreasing order(5 4 3 ... 1 diagnol value)

Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.
We run the "j" loop in increasing order ( i.e from 1 to i )
since rows is in increasing order(1 2 3 ... i single line value)

We print "value" in the inner loop
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;

// Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
// We run the "i" loop in decreasing order ( i.e from 5 to 1 ) since rows is in decreasing order(5 4 3 ... 1 diagnol value)
for(i=5;i>=1;i--)
{
// Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
// We run the "j" loop in increasing order ( i.e from 1 to i ) since rows is in increasing order(1 2 3 ... i single line value)
for(j=1;j<=i;j++)
{
/* Printing "value" in the inner loop since number of columns = number of "value" to be printed.
Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}

// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this```
```return 0;
}
```

8. Program to print the given pattern
```// Program to print the given patter :

/*

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2
( since we have rows and columns. 1 loop for row and 1 loop for column.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.

outer loop "i" runs from 1 to 5 since we have 5 rows.
We run the "i" loop in increasing order
( i.e from 1 to 5 ) since rows is in increasing order(1 2 3 4 5 diagnol value)

Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.
We run the "j" loop in decreasing order ( i.e from 5 to i )
since rows is in decreasing order(5 4 ... i single line value)

We print "value" in the inner loop
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;

/* Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
We run the "i" loop in decreasing order ( i.e from 5 to 1 )
since rows is in decreasing order(5 4 3 ... 1 diagnol value) */

for(i=1;i<=5;i++)
{
/* Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop in increasing order ( i.e from 1 to i ) since rows is in increasing order(1 2 3 ... i single line value) */
for(j=5;j>=i;j--)
{
/* Printing "value" in the inner loop since number of columns = number of "value" to be printed.
Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}

// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this

return 0;
}
```

9. Program to print the given pattern
```// Program to print the given patter :

/*
*
* *
* * *
* * * *
* * * * *

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

--------*
------* *
----* * *
--* * * *
* * * * *

Here ( - ) indicates space before star ( asterik ) to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

The above pattern is similar to pattern :
*
* *
* * *
* * * *
* * * * *

The only difference is we print spaces before the "values".
So design logic according to this and print spaces before that.
Its as simple as you like.

Consider the diagonal to be rows and a single line to be column.

Now earlier we worked only on rows and columns without spaces before the printed "value"
but now we have spcaes as well. So we include another loop for spaces.

Number of loops required = 3
( since we have rows and columns and also spaces. 1 loop for row and 1 loop for column
and 1 loop for spaces.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
One more inner loop will be for printing space.

outer loop "i" runs from 1 to 5 since we have 5 rows.

Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.

Now For spaces : If you notice the spaces are in decreasing manner
( i.e 8 spaces, 6 spaces, 4 spaces, 2 spaces and then none )
We can print the spaces by utilising "i" value
but to simplify the pattern and make it easier we use another value m=8 and
reduce it by 2 after the completion of loop for spaces.
i.e We run the inner loop "s" from 1 to m.

We print "value" in the inner loop
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i, j, s to iterate outer and inner loop respectively
// we are using "i" for rows, "j" for columns ans "s" for spaces.
int i, j, s;

// Initialising m = 8 as total number of space = 8
int m = 8;

// Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
for(i=1;i<=5;i++)
{
// Printing spaces
for(s=1;s<=m;s++)
{
printf(" ");
}
// Reducing value of "m" by 2 to reduce spaces
m=m-2;

/* Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on ) */
for(j=1;j<=i;j++)
{
// Printing "value" in the inner loop since number of columns = number of "value" to be printed.
printf("* ");
}

// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this

return 0;
}
```

10. Program to print the given pattern
```// Program to print the given patter :

/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

--------1
------1 2
----1 2 3
--1 2 3 4
1 2 3 4 5

Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

The above patter is similar to patern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

The only difference is we print spaces before the "values".
So design logic according to this and print spaces before that.
Its as simple as you like.

Consider the diagonal to be rows and a single line to be column.

Now earlier we worked only on rows and columns without spaces before the printed "value"
but now we have spcaes as well. So we include another loop for spaces.

Number of loops required = 3
( since we have rows and columns and also spaces. 1 loop for row and 1 loop for column
and 1 loop for spaces.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
One more inner loop will be for printing space.

outer loop "i" runs from 1 to 5 since we have 5 rows.
We run the "i" loop in increasing order ( i.e from 1 to 5 )
since rows is in increasing order(1 2 3 4 5 diagnol value)

Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.
We run the "j" loop in increasing order ( i.e from 1 to i )
since column is in increasing order(1 2 3 4 5 single line value)

Now For spaces : If you notice the spaces are in decreasing manner
( i.e 8 spaces, 6 spaces, 4 spaces, 2 spaces and then none )
We can print the spaces by utilising "i" value but to simplify the pattern
and make it easier we use another value m=8 and
reduce it by 2 after the completion of loop for spaces.
i.e We run the inner loop "s" from 1 to m.

We print "value" in the inner loop
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i, j, s to iterate outer and inner loop respectively
// we are using "i" for rows, "j" for columns ans "s" for spaces.
int i, j, s;

// Initialising m = 8 as maximum number of space = 8
int m = 8;

// Since we have five rows so we iterate the "i" loop from 5 to 1
/* We run the "i" loop in increasing order ( i.e from 1 to 5 )
since rows is in increasing order(1 2 3 4 5 diagnol value) */

for(i=1;i<=5;i++)
{
// Printing spaces
for(s=1;s<=m;s++)
{
printf(" ");
}
// Reducing value of "m" by 2 to reduce spaces
m=m-2;

/* Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop in increasing order ( i.e from 1 to i )
since column is in increasing order(1 2 ... i single line value) */

for(j=1;j<=i;j++)
{
// Printing "value" in the inner loop since number of columns = number of "value" to be printed.
/* Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}

// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this

return 0;
}
```