C PROGRAM : PATTERN

C PROGRAM : PATTERN

C PROGRAMS : PATTERN
16. Program to print the given pattern
// Program to print the given Pattern

/*

1  
2 6 
3 7 10
4 8 11 13
5 9 12 14 15 ... till n rows

*/

/* 

For example :

For n=3

1
2 4
3 5 6

For n=6

1  
2 7 
3 8 12
4 9 13 16 
5 10 14 17 19
6 11 15 18 20 21

*/

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

 int main()
  { 
  // Declaring variable i, j to iterate loop
 int i, j;    
 
 // Declaring variable n = maximum rows 
 int n;
 
 //Declaring variable k and num to help in generating "values"
 int k, num=1; 
 
 // Inputting Number of rows
 printf("Enter number of rows : ");
  scanf("%d",&n);
 
  // Running loop for rows
  for (i=1; i<=n ; i++) 
  {
     num=i;
     k=n-1;
  
   // printing values
  for (j=1; j<=i; j++) 
      {
       // %-4d = reserves 4 spaces for printing "value" to obtain clarity in pattern
 printf("%-4d",num);
        num=num+k;
        k=k-1;    
      } 
      
   printf("\n");    
 }
        
     getch(); // Linux user - Remove this

     return 0; 
  } 


17. Program to print the given pattern
// Program to print pattern : Hollow Diamond

/* 

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

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

LOGIC :

BREAKING IT UP : It has 2 parts

First part :

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

Which is further divided into : 

First Sub Part :

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

Second Sub part :

******** 
 ******* 
  ****** 
   *****  
    ****             
     ***               
      **
       *
       
And ofcourse the spaces before it.

Second Part :

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

Which is further divided into :

First Sub Part :

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

Second Sub Part :

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

And the spaces before it.

*/

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

int main()
{
 // Declaring variable i, j, s to iterate loop 
 int i, j, s;
 
 // Declaring ms = maximum space
 int ms = 1;
 
 // Running loop for rows for first part
 for(i=1;i<=8;i++)
 {
  // Printing first Sub Part :
  for(j=8;j>=i;j--)
  {
   printf("*");
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Incresing spaces
  ms=ms+2;
  
  // Printing Second Sub Part :
  for(j=8;j>=i;j--)
  {
   printf("*");
  }
  
  printf("\n");
  
 }
 
 printf("\n");
 // Initialising ms = 15 i.e the maximum space
 ms = 15;
 
// Running loop for Second Part
 for(i=1;i<=8;i++)
 {
  // Printing first Sub Part :
  for(j=1;j<=i;j++)
  {
   printf("*");
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing spaces
  ms=ms-2;
  
  // Printing Second Sub Part :
  for(j=1;j<=i;j++)
  {
   printf("*");
  }
  
  printf("\n");
  
 }

    getch(); // Linux user - Remove this

    return 0;
}

18. Program to print the given pattern
/*

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

BREAKING IT UP : The above pattern has two parts

First Part :

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

Second Part :

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

 */

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

int main()
{
 // Declaring variable i, j, s to iterate loop
    int i,j,s;
    
    // Declaring variable ms = maximum space
    int ms = 5;
    
    // Declaring variable to print "*" ( asterik )
    int as = 1;
    
    // Running loop for First part
    for(i=1;i<=5;i++)
    {
     // Printing Space
     for(s=1;s<=ms;s++)
     {
      printf(" ");
     }
     
     // Reducing spaces
     ms--;
     
     // Printing First Part
     for(j=1;j<=as;j++)
     {
      printf("*");
     }
     
     // Increasing Asterik ( "*" )
     as=as+2;
     
     printf("\n");
    }
    
 // Re-initialising ms = maximum space and aterik
 ms=2;
 as=7;
 
    // Running loop for Second Part
     for(i=1;i<=5;i++)
    {
     // Printing Space
     for(s=1;s<=ms;s++)
     {
      printf(" ");
     }
     
     // Increasing spaces
     ms++;
     
     // Printing First Part
     for(j=1;j<=as;j++)
     {
      printf("*");
     }
     
     // Decreasing Asterik ( "*" )
     as=as-2;
     
     printf("\n");
    }
    
getch(); // Linux user - Remove this

return 0;
}

19. Program to print the given pattern
/*

8 4 2 1
4 2 1
2 1
1

 */

#include<stdio.h>
#include<conio.h>
int main()
{
 int n=8,m;
 int i,j;
 m=n;
 
 for(i=4;i>=1;i--)
 {
  for(j=1;j<=i;j++)
  {
   printf("%d ",m);
   m=m/2; 
  }
  m=n/2;
  n=n/2;
  printf("\n");
 }
 
 getch();
 return 0;
}

20. Program to print the given pattern
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
        int arr[30],temp[30],i,j,k,l,n;
     
       printf("Enter the number of lines to be printed: ");
       scanf("%d",&n);

       temp[0]=1;
       arr[0]=1;

       for(j=0;j<n;j++)
       printf(" ");

       printf(" 1\n");

       for(i=1;i<n;i++)
       {
               for(j=0;j<n-i;j++)
       printf(" ");

       for(k=1;k<i;k++)
       {
                  arr[k]=temp[k-1]+temp[k];      
        }

                arr[i]=1;

                for(l=0;l<=i;l++)
       {
                 printf("%2d",arr[l]);
                 temp[l]=arr[l];
        }


                printf("\n");
        }

getch(); // Linux user - Remove this

return 0;

}

More Informative posts :




C PROGRAM : PATTERN

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 :




C PROGRAMS : PATTERN

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;
}

More Informative posts :

C PROGRAMS : PATTERN

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;
}

More Informative posts :

Other References :