java program to print star pattern rectangle

Pattern programs are the most common programs when it comes to any programming language such as java, c or c++. Specially if you are weak in for loop then the best way to master loop concept is to practice pattern programs as much as you can. In this post, I am going to discuss the simplest pattern program and its logic. And later on, I will be covering almost all kind of pattern program which will be help full for you to draw any kind of pattern.

So our first program is a simple rectangle box.

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

About this program

This program draws a rectangle box using stars, which consists of five rows and five columns.

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

Logic

To draw a pattern we are using two for loops. One loop for row and one for columns. After printing a row the cursor goes to the new line and prints another row.

Code

OUTPUT

Code Explanation

It will give the following output: * * * * *

Here the inner loop is used to print the number of columns. First j’s value is 1. Then it checks the condition if j is less than or equal to 5. If the condition satisfies it goes inside the loop, prints the * and then j’s value is incremented by 1.

Now j’s value is 2. It again checks the condition if the value of j is less than or equal to 5 or not. If it is less than or equal to 5, again it goes inside the loop and prints the * and then again the value of j is incremented by 1. Now j’s value is 3. This way it prints five stars.

Now one row is complete. Which consists five columns. To complete the pattern we can repeat the row five times.

To print another four rows we can copy the above-discussed loop for another four times, right? But the easier way to copy the row for another four times is to use another for loop which repeats the inner loop five times. To do this we can do something like this:

After doing this the output will be: ***** ***** ***** ***** *****

So after the inner loop we are printing a new line using System.out.println(); to move the cursor to the next line, whenever it goes out of the inner loop.

Now the output will be:

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

So how does it work?

At first, the i’s value is 1. It checks the condition if it is less than equal to 5 or not. If the condition satisfies it goes inside the loop. Then it goes into the inner loop and then the inner loop process continues as discussed above.

After completing the inner loop, the cursor goes to the next line. Now the i’s value is incremented by 1 and the value of i is 2. It again checks the condition of the outer loop, if the condition satisfies, again it enters the inner loop and the process continues until the value of i of the outer loop does not satisfy the condition.

c1 c2 c3 c4 c5
r1   *   *   *   *   *
r2   *   *   *   *   *
r3   *   *   *   *   *
r4   *   *   *   *   *
r5   *   *   *   *   *

Hope you guys understood the logic. If you have any doubt or any kind of suggestion don’t forget to comment in the comment section.

1 – Java program to print star pattern (Rectangle)
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *