Tuesday, February 23, 2021

How To Print Patterns In Java?: A beginners Guide

How To Print Patterns In Java? A Beginners Guide

The pattern programs in Java helps in enhancing the coding skill, logic building, and how the looping concepts works. It's mostly asked by the interviewer in Java interview to check the logic and thinking of the programmer. One can print patterns in  several ways and make numerous designs in a Java program. To learn the program, you must have good command in the knowledge of the Java loop, such as for loop, while loop, and do-while loop. In this section, we'll learn how to print a pattern in Java. 
Let's classified the Java pattern program into three basic categories and you can pick any of it. 
  • Star Pattern 
  • Number Pattern 
  • Character Pattern 
Before moving to the pattern programs, let's see how we are going to approach this problem statement.


When it comes to designing the logic for a pattern program, the  first thing is to draw that pattern in the blocks or on the paper, as we have shown in the above image. This figure of pattern presents a clear look that help us to understand how the loop is going to work..

Each pattern program has two or more than that. The number of the loop depends on the complexity of the pattern or logic. The first 'for' loop will work for rows and the second loop will work for columns. Typically, most people uses Java language for creating pattern programs especially using the For Loop.

Let's build the logic first for the given pattern. In the following snippet, we are starting from the row and column value by takin them as zero "0". We can also start it from 1, it's completely up to your choice.

1
2
3
4
5
6
for(int i=0; i<row; i++){   
    for(int j=0; j<=i; j++){   
        System.out.print("* ");   
    }   
    System.out.println();   
}

Here, in the above snippet, you are seeing a few line of code, where the first for loop will define the row and the second for loop has been used to define columns.

Let's see what could be outcome of this after execution of the code step-by-step, for n=4 (the number of rows we want to print).

In the first iteration:

For i=0, 0<4 (true)
For j=0, j<=0 (true)
The first print statement prints a star(*) at the first row and the second println statement prints the spaces and shifts the cursor to the next line.
*
Now, the value of i and j is changed or increased to 1.

In iteration 2:

For i=1, 1<4 (true)
For j=1, 1<=1 (true)

The first print statement prints two stars(* *) at the second row and the second println statement prints the spaces and shifts the cursor to the next line. 

1
2
*  
* *  

Now the value of i and j is changed or increased to 2.

In iteration 3:

For i=2, 2<4 (true)
For j=2, 2<=2 (true)

Similarly following the loop, here the first print statement prints three stars (* * *) on the 3rd row and the second println statement prints the spaces and shifting the cursor-pointer to the next line.

1
2
3
*  
* *  
* * *  

Now the value of i and j is changed or increased to 3.

In iteration 4:

For i=3, 3<4 (true)
For j=3, 3<=3 (true)

The first print statement prints four stars(* * * *) at the fourth row and the second println statement prints the spaces and shifts the cursor to the next line.

1
2
3
4
*  
* *  
* * *  
* * * *  

Now the value of i and j is changed or increased to 4.

For i=4, 4<4 (false)

The execution of the program will automatically be terminated by the compiler when it finds the value of i equals to the number of rows.

Now you know the basic concept behind creating a pattern in java or in any other language. The logic is same everywhere but the format varies from language to language.

I hope this article will help you and if you liked it then please let me know.

You can comment your thoughts down below. 

Thanks!

How To Print Patterns In Java?: A beginners Guide

How To Print Patterns In Java? A Beginners Guide The pattern programs in Java helps in enhancing the coding skill, logic building, and how t...