C PROGRAMS : PATTERN

C PROGRAMS : PATTERN
1. Program to print the given pattern
```/*

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

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

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.

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 1 to "i".

We print "*" in the inner loop since number of columns = number of star 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 1 to 5 ( i.e 5 times )
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 )
so we run the "j" loop i.e the inner loop from 1 to "i". */
for(j=1;j<=i;j++)
{
// Printing "*" in the inner loop since number of columns=number of star to be printed
printf("* ");
}

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

getch(); // Linux user - Remove this

return 0;
}
```

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

/*

*
* *
* * *
* * * *
* * * * *
* * * * * * ..... till n rows

Here n = any integer value entered by user.
n represents rows.

Example :

For n=3

*
* *
* * *

For n=5

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

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

In above Pattern Number of rows = n

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 n since we have n 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 )
so we run the "j" loop i.e the inner loop from 1 to "i".

We print "*" in the inner loop since number of columns = number of star 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;

//Declaring Variale n=rows
int n;

// Entering number of rows from user
printf("Enter number of rows : ");
scanf("%d",&n);

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

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

getch(); // Linux user - Remove this

return 0;
}
```

3. Program to print the given pattern
```// NOTE : For Better understanding read both LOGIC and COMMENTS

// Program to print the given patter :

/*

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 .... till n rows

Here n = any integer value entered by user.
n represents rows.

Example :

For n=2

1
1 2

For n=6

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

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

In above Pattern Number of rows = n

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 n since we have n rows.
We run the "i" loop in increasing order ( i.e from 1 to n )
since rows is in increasing order(1 2 3 ... n 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 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 ... i single line value )

We print "value" in the inner loop since number of columns = number of "values" 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;

//Declaring Variale n=rows
int n;

// Entering number of rows from user
printf("Enter number of rows : ");
scanf("%d",&n);

// Since we have five rows so we iterate the "i" loop from 1 to 5
for(i=1;i<=n;i++)
{
/* Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column and so on )
so we run the "j" loop from 1 to "i" */
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. */
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;
}
```

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

/*

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5 .... till n rows

Here n = any integer value entered by user.
n represents rows.

Example :

For n=3

1
2 2
3 3 3

For n=6

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

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

In above Pattern Number of rows = n

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 n since we have n 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 )
so we run the "j" loop i.e the inner loop from 1 to "i".

We print "value" in the inner loop
since number of columns = number of "values" 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;

//Declaring Variale n=rows
int n;

// Entering number of rows from user
printf("Enter number of rows : ");
scanf("%d",&n);

// Since we have five rows so we iterate the "i" loop from 1 to 5
for(i=1;i<=n;i++)
{
/* Notice in above pattern Number of columns = Position of rows
( i.e 1st row has 1 column, 2nd row has 2 column and so on )
so we run the "j" loop from 1 to "i" */

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 "i"
since value to be printed is the position of the row. */

printf("%d ",i);
}

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

getch(); // Linux user - Remove this

return 0;
}
```

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

/*

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

*/

/*

REMEMBER : We always design logic according to the pattern.

LOGIC:

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.

Inner loop also runs from 1 to 5 since we have 5 columns in each row.
so we run the "j" loop i.e the inner loop from 1 to 5.

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 1 to 5
for(i=1;i<=5;i++)
{
// We have five columns as well so we iterate the "j" loop from 1 to 5.
for(j=1;j<=5;j++)
{
/* Printing "value" in the inner loop
since number of columns = number of "value" to be printed.
Here we print the value of "i"
since value to be printed is the position of the row. */

printf("%d ",i);
}

/* After completion of printing "*" in row
we need to go to the next row. */

printf("\n");
}

getch(); // Linux user - Remove this

return 0;
}
```

Other References :

Previous
Next Post »