C PROGRAM : PATTERN

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

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


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

--------5
------4 5
----3 4 5 
--2 3 4 5
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:
5
4 5
3 4 5 
2 3 4 5
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.

Above pattern is different from the pattern we have been printing.
Notice the diagonal values are all same ( i.e 5 - neither increasing nor decreasing ) and
Also the initial column value is changing. So what do we do????

Simple, Since diagonal values are all same ( i.e 5 - neither increasing nor decreasing ) 
so we consider ( 5 4 3 2 1 ) as diagonal values.
So, we run "i" loop i.e the outer loop from 5 to 1 
as the diagonal values are in decreasing order according to our assumption.

Also if you notice the single line value( i.e column value ) is in increasing order 
the only catch is this time the initial "value" is changing and the last "value" is fixed. 
Earlier it was just the reverse of it.
So, now we run "j" loop i.e the inner loop from "i" to 5 as last value is always 5.

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 ( i.e 5 times )
We run the "i" loop in decreasing order ( i.e from 5 to 1 ) 
since rows is in increasing order(5 4 3 2 1 diagnol value - according to our assumption of diagonal values ) */
for(i=5;i>=1;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 i to 5 ) since column is in increasing order. */
  
for(j=i;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 "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;
}

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

// Program to print the given patter :

/* 

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

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

Example :

For n=2

1
* *

For n=6

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

*/

/* 

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 3  5 ... 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 )

Now every time row%2 == 0 we print "*" instead of the regular values.

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 ( i.e 5 times )
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, 3rd row has 3 columns 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. */
  
if(i%2==0)
printf("* ");
else
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;
}

13. Program to print the given pattern
// Program to print the given patter :

/* 

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


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

We break the pattern in parts : In this case in three parts.

First part :
1
1 2
1 2 3
1 2 3 4 
1 2 3 4 5

Second part : It is actually the spaces that we print before the third part.
----------------
  ------------
    --------
      ----  
      
Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

Third part :
        1
      1 2
    1 2 3
  1 2 3 4
1 2 3 4 5

The above patter ( i.e the third part ) 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.
I am referring to the second part spaces. Its as simple as you like.

In earlier example I have explained enough so from now on I will not go in details.

Number of loops required = 4, one outer loop and the three inner 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, k, s 
 to iterate outer and the three inner loop respectively */
 int i, j, k, s;
 
 // Initialising ms = 16 as maximum number of space = 16
 int ms = 16;
 
 // Running loop for rows
 for(i=1;i<=5;i++)
 {
  
  // Printing the First part : 
  for(j=1;j<=i;j++)
  {
   printf("%d ",j);
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing value of "m" by 4 to reduce spaces
  ms=ms-4;
  
  // Printing the third part :
  for(k=1;k<=i;k++)
  {
   printf("%d ",k);
  }
  
  /* After completion of printing "value" in row 
  we need to go to the next row.*/
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program to print the given pattern
// Program to print the given patter :

/* 

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


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

We break the pattern in parts : In this case in three parts.

First part :
1
1 2
1 2 3
1 2 3 4 
1 2 3 4 5

Second part : It is actually the spaces that we print before the third part.
----------------
  ------------
    --------
      ----  
      
Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

Third part :
        1
      2 1
    3 2 1
  4 3 2 1
5 4 3 2 1

The above patter ( i.e the third part ) is similar to patern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

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

In earlier example I have explained enough so from now on I will not go in details.

Number of loops required = 4, one outer loop and the three inner 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, k, s 
 to iterate outer and thre three inner loop respectively */
 int i, j, k, s;
 
 // Initialising ms = 16 as maximum number of space = 16
 int ms = 16;
 
 // Running loop for rows
 for(i=1;i<=5;i++)
 {
  
  // Printing the First part : 
  for(j=1;j<=i;j++)
  {
    printf("%d ",j);
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing value of "m" by 4 to reduce spaces
  ms=ms-4;
  
  // Printing the third part :
  for(k=i;k>=1;k--)
  {
   printf("%d ",k);
  }
  
  /* After completion of printing "value" in row 
  we need to go to the next row. */
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

15. Program to print the given pattern
// Program to print the given patter :

/* 

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


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

We break the pattern in parts : In this case in three parts.

First part :
1
1 2
1 2 3
1 2 3 4 
1 2 3 4 5

Second part : It is actually the spaces that we print before the third part.
----------------
  ------------
    --------
      ----  
      
Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

Third part :
        5
      5 4
    5 4 3
  5 4 3 2
5 4 3 2 1

The above patter ( i.e the third part ) is similar to patern:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

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

In earlier example I have explained enough so from now on I will not go in details.

Number of loops required = 4, one outer loop and the three inner 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, k, s 
 to iterate outer and thre three inner loop respectively */
 int i, j, k, s;
 
 // Initialising ms = 16 as maximum number of space = 16
 int ms = 16;
 
 // Initialising variable to help print third part tp = 5
 int tp = 5;
 
 // Running loop for rows
 for(i=1;i<=5;i++)
 {
  
  // Printing the First part : 
  for(j=1;j<=i;j++)
  {
    printf("%d ",j);
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing value of "m" by 4 to reduce spaces
  ms=ms-4;
  
  // Printing the third part :
  for(k=5;k>=tp;k--)
  {
   printf("%d ",k);
  }
  //Reducing value of tp = variable used to print third part
  tp--;
  
  /* After completion of printing "value" in row 
  we need to go to the next row. */
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

More Informative posts :




Share this

Related Posts

Previous
Next Post »