Pattern Programming in C

Pattern programs are very interesting type of programs that are done in C by implementing various types of loops that makes our job quite interesting.

We follow the same basic algorithm for dealing with most of them:

  • Increment column wise or simply get a loop to step down a line
  • Increment row wise or rather increment in a single line

generally these increments are done with the help of loops.

Lets discuss some of the very useful types of pattern programmings:

Pattern Type 1:

Lets see the output of this program first:

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

Now in the above output we see a number of stars coming up in a specific order or arrangement. We see that up-to 4th column the number of stars are gradually increasing one by one and then in a same way decreasing up-to 1. So it is very important to get the proper idea of the way the program runs. So the Algorithm comes as:

  • Use a for loop to increment one line
  • Use another nested loop to print ‘*’ within each loop or line
  • Same as use decrements after line line 4 to print ‘*’.

So lets see the code:

// Pattern 1 type programming
#include <stdio.h>

int main(){

int i,j,k;

 for(i=1;i<=4;i++)//loop for increment the line
 {

 for(j=1;j<=i;j++)// loop for printing the star  {   printf("*"); }  printf("\n");  }   for(i=3;i>=1;i--)// loop for increasing line
 {

 for(j=1;j<=i;j++)// loop for decreasing star
 {
 printf("*");
 }
 printf("\n");

}
 return 0;
}

Pattern Type 2:

Similar to the previous type lets see the output first:

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

So now it is also a similar type of pattern quite similar but having a very new style in it. Here we can see the spaces coming before every line has been used in quite a format this is what we call ‘space management’ in pattern programming. Simple Algorithm for this program:

  • print maximum ‘space’ and minimum star in 1st line and then proceed the line
  • decrease the number of ‘space’ and increase the number of star by 1 in each line
  • while in second part give a constant number of space in every line
  • decrease number of stars one in each line
  • no space management in second part is rather necessary

So lets check out the code for this one:

// Program for pattern 2
#include <stdio.h>

void main(){

int i,j,k;

for(i=1;i<=4;i++)//loop for line  { for(j=(4-i);j>=1;j-=1)//loop for space
 {
 printf(" ");
 }
 for(k=1;k<=i;k+=1)// loop for *  {  printf("*");  }  printf("\n");  } for(i=4;i>=1;i--)
 {
 printf(" ");

 for(k=1;k<=i;k++)
 {
 printf("*");
 }
 printf("\n");
 }
}

Pattern Type 3:

Check out the output of this program:

1      1
22    22
333  333
44444444

This is a bit harder show of space management. The space is fist printed maximum times and then decreased by two in each line.Simple algorithm that we follow here is:

  • Get two nested loops in each column for printing the numbers
  • Print spaces for maximum times in 1st column and then gradually decrease it by 2
  • adjust such that no spaces remains in last line.

Thus we see the code:

//Pattern programming 3
#include <stdio.h>

int main(void)
{

	int i,j,k,count=6;

	for(i=1;i&lt;=4;i++)
	{
		for(j=1;j&lt;=i;j++)
		{
		printf("%d",i);
		}
		for(k=1;k&lt;=count;k++)
		{
		printf(" ");
		}

		for(j=1;j&lt;=i;j++)
		{
		printf("%d",i);
		}
	count=count-2;
	printf("\n");

	}
}

Pattern Type 4:

This is most important or most common type of pattern that we see.This is often called the TREE program by programmers.

As usual lets see the output first:

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

The algorithm:

  • use a loop for increment column wise
  • decrease the number of space gradually in each row
  • use a specific formola : (2*row – 1) to print * in each row

Now we see the code of this program:

//Pattern programming 3
#include <stdio.h> 

int main()
{
   int row, c, n=6, temp;
   temp = n;

   for ( row = 1 ; row &lt;= n ; row++ )
   {
      for ( c = 1 ; c &lt; temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c &lt;= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

   return 0;
}

Pattern Type 5:

This is a program to get pattern style using characters. Nothing much new in the style as we were decreasing number of spaces here we will just be increasing it.

The output comes as:

A B C D E F 
 A B C D E 
  A B C D 
   A B C 
    A B 
     A

The simple algorithm that we follow here is:

  • just increase number of left side spaces in gradual steps
  • decrease the number of printing characters

Code:

//Pattern programming 
#include <stdio.h> 

main()
{
      char ch = 'A';
      int n=6, c, k, space = 0;

      for ( k = n ; k &gt;= 1 ; k-- )
      {
          for ( c = 1 ; c &lt;= space ; c++)
              printf(" ");

          space++;

          for ( c = 1 ; c &lt;= k ; c++ )
          {
             printf("%c ", ch); 
             ch++;
          }

          printf("\n");
          ch = 'A';
      }

      return 0;
}

Pattern Type 6:

This pattern or the Diamond pattern is basically not only the reconstruction of the Tree pattern but rather constructing it twice .. once increasing and then decreasing number of stars in each row.

So the output comes as:

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

Simple algorithm of it:

  • Same as the tree pattern print once the stars
  • then reverse the conditions for the second half

Code:

//Pattern programming Diamond type
#include <stdio.h> 

int main() {

	int rows=4, a, b, space;

	// Print first half of the triangle.
	space = rows - 1;
	for ( b = 1 ; b &lt;= rows ; b++ ) {
		for ( a = 1 ; a &lt;= space ; a++ )
			printf(" ");
		space--;
		for ( a = 1 ; a &lt;= 2*b-1 ; a++)
			printf("*");
		printf("\n");
	}

	// Print second half of the triangle.
	space = 1;
	for ( b = 1 ; b &lt;= rows - 1 ; b++ ) {
		for ( a = 1 ; a &lt;= space; a++)
			printf(" ");
		space++;
		for ( a = 1 ; a &lt;= 2*(rows-b)-1 ; a++ )
			printf("*");
		printf("\n");
	}
	return 0;
}

All the programs mentioned above can be changed for using as any number of rows or columns the user wants just by using scanf().

Happy Coding!!

Leave a comment