In this post I will explain the logic of
printing different types of patterns. This post will clear all doubts for
printing patterns and after this post you can print any type of patterns.
The most important thing about printing patterns
is the logic, the way it can be printed, we just have to learn that logic after
we get the idea of logic, we can print it using any programming language.
For this post I am using Java language, but
as I said logic is important, if you are able to grab that logic then you can
use any programming language to print those patterns.
First of all try to think a rectangular
space for printing patterns which can be divided to 4 parts, following image
shows that division:
So in above figure we have 4 parts, so
while we will be printing something we will take these areas into
consideration. For printing those parts we will be using for loop for each part.
For example suppose if we are going to
print the following pattern
1
12
123
1234
12345
Then we are concerned about only first
part.
And if we are going to print the following
pattern
Then we are concerned about all 4 parts
In first part we have to print that number
part after that in second and third part we have to print spacing part and in
fourth part we again have to print that number part.
So in this way we will be printing the
patterns.
Let’s print our first pattern which is
1 (1)
12
(2)
123
(3)
1234
(4)
12345
(5)
For printing patterns first thing we need
to know the number of lines we are going to print. In above pattern we have 5
lines so we will be using a for loop for tracking those lines.
·
For(int i=1,i<=5;i++)
After that since in above pattern we are
concerned about first part only so we need only one for loop.
Now for first time when the outer loop execute
then at that time the value of i would be 1 and we also have to print 1 in
first line , for second time we have to print 1 and 2 and at that time value of
I would be 2. So I think we can create some sequence. Suppose for second loop
we will be using variable j then sequence is
·
For first row I=1 ==> we have to print j=1
·
For second row I=2 ==> we have to print j=1 2
·
For third row I=3 ==> we have to print j=1 2 3
·
For fourth row I=4 ==> we have to print j=1 2 3 4
·
For fifth row I=5 ==> we have to print j=1 2 3 4 5
That’s it
We have created some type
of relationship, for each row we have to print the value of j which will start
from 1 and go till the value of i.
·
for(int j=1;j<=i;j++){
·
System.out.print(j);
And after completing 1
inner loop for value of j we have to change the line for that we will use this
statement
·
System.out.print();
So in this way the above pattern will be
printed.
Program for that pattern is following:
Program Code:
public class SimplePattern {
Output:
public class SimplePattern {
public static void main( String arg[]){
//for row counts
for(int i=1;i<=5;i++){
//for printing numbers
for(int j=1;j<=i;j++){
System.out.print(j);
}
//for entering to new line
System.out.println();
}
}
}
Output:
1
12
123
1234
12345
So in above pattern how any loops will be
required????
·
1 for row count
·
4 for printing 4 parts
·
Total 5 loops
·
Outer Loop : for(int i=1;i<=5;i++) Since 5 rows are there
·
For 1st part for(int
j=1;j<=i;j++){
·
System.out.print(j);
First part I have already explained how to
print.
So now I will explain second part, for this
part we will be using variable k
Let’s try to establish some relationship
·
Initially we will need 4
spaces, in second row we will need 3 spaces for next row we will need 2 and so
on
·
For this we will take one
variable in outer loop and we will initialize its value with 4 and decrement
it.
·
Our modified outer loop for(int i=1,z=4;i<=5;i++,z--)
·
Our for loop for second part is for(int k=z;k>=1;k--){
·
System.out.print(" ");
·
}
Now we will try to print
our 3rd part which is also equivalent to our second part so we will
need same for loop values
·
for(int l=z;l>=1;l--){
·
System.out.print(" ");
·
}
That’s it now we need our 4th part in which we have to
print the values just like first part. So for loop for this part will be like 1st
part
·
for(int m=1;m<=i;m++){
·
System.out.print(m);
·
}
So that’s it we are done.
Following is the program
to print the above pattern
Program Code:
public class SimplePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1,z=4;i<=5;i++,z--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
for(int k=z;k>=1;k--){
System.out.print(" ");
}
for(int l=z;l>=1;l--){
System.out.print(" ");
}
for(int m=1;m<=i;m++){
System.out.print(m);
}
System.out.println();
}
}
}
We have also seen patterns
which will be towards downside direction like
12345
1234
123
12
1
And many more same logic can be applied
there too.
So I am showing only program an output if
there is any confusion or if you are not able to understand then I will also
explain those patterns.
Program Code:
public class SimplePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=5;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
Output:
12345
1234
123
12
1
Program Code:
public class SimplePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
for(int i=4;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
Output:
1
12
123
1234
12345
1234
123
12
1
Program Code:
public class SimplePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1,z=4;i<=5;i++,z--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
for(int k=z;k>=1;k--){
System.out.print(" ");
}
for(int l=z;l>=1;l--){
System.out.print(" ");
}
for(int m=1;m<=i;m++){
System.out.print(m);
}
System.out.println();
}
for(int i=4,z=1;i>=1;i--,z++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
for(int j=1;j<=z;j++){
System.out.print(" ");
}
for(int j=1;j<=z;j++){
System.out.print(" ");
}
for(int m=1;m<=i;m++){
System.out.print(m);
}
System.out.println();
}
}
}
Output:
So above is all about patterns if there is
any confusion or you want to print some other patterns please comment. I will
try to explain anything you want.
Thanks for reading this post.
0 comments:
Post a Comment