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 :




Share this

Related Posts

Previous
Next Post »