Pattern Printing

Pattern printing in any programming language is the most difficult job for the beginners. Because there is no exact algorithm available to print a pattern in any programming language, It is all about common sense and ability of your mind to solve an issue.

 

But here we are with a easiest trick of pattern printing. for this you should be aware of only use of simple graph.

 

So to print a pattern in any programming language please follow the steps:-

 

First of all pick a pen and paper and draw a graphical representation of lines or just keep a graph paper to you and mark the position of co-ordinates(set the value of X and Y co-ordinates according to your need).

1

 

or

 

Simply write the co-ordinate value on a paper

2.jpg

 

  1. Now, suppose we have to print a ‘W’ pattern. for this join the co-ordinates that makes the letter ‘W’.

 

3.jpg

 

The co-ordinates are (0,0  1,1  2,2  3,3  2,4  1,5  0,6  1,7  2,8  3,9  2,10  1,11  0,12)  makes letter ‘W’. this can be divide into 4 sub-parts

 

\ First part of W

/Second part of W

\ Third part of W

/Fourth part of W

 

 

  1. For the first part of W try to established a relation among its coordinates i.e. 0,0 1,1  2,2  3,3-

 

Here the value of  X and Y are equal i.e. X=Y

 

So the first condition is if(X==Y)                     (I)

 

 

 

  1. For the second half of letter W i.e.among co-ordinates   3,3  2,4  1,5  0,6 make a relation-

 

Here The sum of X and Y co-ordinate is 6 i.e. X+Y=6

 

So the second condition is if((X+Y)==6)                 (II)

 

 

 

  1. For the third half of letter W i.e. among co-ordinates 9 0,6  1,7  2,8  3,make a relation-

 

Here the subtraction of X from Y is 6 i.e. Y-X=6

 

So the third condition is if((Y-X)==6)                        (III)

 

 

 

  1. For the fourth half of letter W i.e. among co-ordinates 3,9  2,10  1,11  0,12 try to established a relation-

 

Here sum of X and Y co-ord. is 12

 

So the fourth condition is if((X+Y)==12)                               (IV)

 

Now use all these four conditions in the program and print W. The JAVA code is given below:-

————————————————————————–

public class WpatternPrint {

public static void main(String …arg)

{

 

for(int i=0;i<4;i++)

{

for(int j=0;j<12;j++)

{

if(i==j)

{

System.out.print(“*”);

}

else if(((j+i)==6))

{

System.out.print(“*”);

}

else if(((j-i)==6))

{

System.out.print(“*”);

}

else if(((j+i)==12))

{

System.out.print(“*”);

}

 

else

{

System.out.print(” “);

}

}

System.out.println();

}

}

}

 

Output

———————————————–

4

————————————————————————–

 

 

 

Using this simple way any pattern can be printed very easily because it helps to establish a relation among its coordinates.

 

So follow the steps carefully and feel free from the burden of pattern printing.  🙂

 

——————————————————————————————————————————————————————————————————-

 

It depends upon you that which method is easy to use either graph or simple co-ordinate but I would like to advice please use the graph mode to print the Naming pattern, because it looks better and will be easy to understand. e.g-

 

5

 

Measure the X and Y co-ordinates carefully and make a relation among its co-ordinates, use this relation as a condition and use it in program to print any pattern.

 

JAVA Code of this figure(RANJEET) is given below-

 

class NamePattern

{

public static void main(String …arg)

{

int i,j;

for(i=0;i<=8;i++)

{

for(j=0;j<=100;j++)

{

if((i==0) && ((j>=0)&&(j<=4)))

{

System.out.print(“*”);

}

else if(j==0)

{

System.out.print(“*”);

}

else if((i==4) && ((j>=0) && (j<=4)))

{

System.out.print(“*”);

}

else if((j==4) && ((i>=0) && (i<=4)))

{

System.out.print(“*”);

}

else if(((i-j)==4))

{

System.out.print(“*”);

}

else if((j==9) || (j==13))

{

System.out.print(“*”);

}

else if(/*upper line*/((i==0) && (j>=9) && (j<=13)) ||/*lower line*/ ((i==4) && (j>=9) && (j<=13)))

{

System.out.print(“*”);

}

else if((j==18) || (j==22) || (j==26))

{

System.out.print(“*”);

}

else if(((i==0) && ((j>=18) && (j<=22))))

{

System.out.print(“*”);

}

else if(((i==8) && ((j>=22) && (j<=26))))

{

System.out.print(“*”);

}

else if(((j==36) || ((i==8) && (j>=31) && (j<=36))))

{

System.out.print(“*”);

}

else if(((j==31) && ((i>=5) && (i<=8))))

{

System.out.print(“*”);

}

else if((j==41) || (j==51))

{

System.out.print(“*”);

}

else if((i==0) && ((((j>=41) && (j<=46))) || ((j>=51) && (j<=56))))

{

System.out.print(“*”);

}

else if((i==3) && ((((j>=41) && (j<=46))) || ((j>=51) && (j<=56))))

{

System.out.print(“*”);

}

else if((i==8) && ((((j>=41) && (j<=46))) || ((j>=51) && (j<=56))))

{

System.out.print(“*”);

}

else if((j==64) || ((i==0) && (j>=61) && (j<=67)))

{

System.out.print(“*”);

}

 

else System.out.print(” “);

}

System.out.println();

 

}

 

}

}

 

————————————————————————–——-

 

Output-

 

————————————————————————–——-

6

————————————————————————–——-

 

 

 

I hope this article will help you to understand the concept of pattern printing in any programming  language.

 

 

 

****thank you****

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s