2 – Java program to print hollow star pattern (Rectangle / Square)

Java star pattern
Java star pattern
In the previous post, I have discussed some basics of pattern programs and how pattern programs work. If you are a beginner (like me) and have not seen my previous post I suggest you to please check Java program to print star pattern before continuing further.

In this article, I am going to show you how to draw a slightly different star pattern. A hollow rectangle or square pattern like this:

The logic of both the hollow rectangle star pattern and the square pattern are same. You just have to print spaces to make it look like a square.

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

So basically, if we print spaces between each column it will look like a square, otherwise, it will look like a rectangle as shown above.

About this program

Here I am going to show you two programs. One that doesn’t take input from the user and another one that takes input. Based on the input the pattern will be printed.

At first, let’s see the the program that doesn’t take input from the user –

Code

OUTPUT

Code Explanation

Here the logic is same as previous program 1 – Java program to print star pattern. The only difference is that it is a hollow pattern.
java-star-pattern-program

From the above picture, we can see that we have to print only the row 1, row 5 and column 1, column 5 to get the desired pattern. Hence to meet this condition we have used if..else“.

Here we already know that i=row and j=column. So we can do like this:

If i is equal to 1 or i is equal to 5 or j is equal to 1 or j is equal to 5, then print “*” else print a blank space.

More programs on Java

Number hollow star pattern

Program to count number of words in a string (multiple spaces) 

Program which takes input from the user. The logic is same as above.

Hope you understood the logic. If you still have any doubt, please let us know in the comment section. You can also request a program.

* We have made an Android app for you. Programming Mate, get it on Google Play. The Programming Mate app contains more than 150 pattern programs. Get it here.

 

You May Also Like

15 Comments

  1. I want to writ a hollow square within the first row a title “Rechthoek”.
    The title has to be in the center.

    **Rechthoek**
    * *
    ****************

  2. I need a pgm
    Enter a character. Enter length and breadth. An d print hollow pattern with char.
    Eg.
    L=6
    B=4
    Char=@
    Output:
    @@@@@@
    @ @
    @ @
    @@@@@@

    1. not quite sure if this is what you want, but still can be used somehow
      import java.util.*;

      public class SquareOfStars {

      public static void main(String args[]) {
      int rows, i, j;

      Scanner in = new Scanner(System.in);
      System.out.println(“Enter number of rows in square pattern”);
      rows = in.nextInt();

      /* Row iterator for loop */
      for(i = 0; i < rows; i++){
      /* Column iterator for loop */
      for(j = 0; j <=rows-2; j++){
      if ((i%2==0)&&(j==rows-2)) {
      System.out.printf("*");
      }

      System.out.print("*");
      }
      System.out.print("\n");
      }
      }

      }

  3. Code for this:

    *
    / \
    * *
    / \
    * – * – * – * – * – * – *
    \ / \ /
    * * * *
    \ / \ /
    * *
    / \ / \
    * * * *
    / \ / \
    * – * – * – * – * – * – *
    \ /
    * *
    \ /
    *

Leave a Reply

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