Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Hollow Square Star Pattern in Java

Last Updated on May 18, 2023 by Prepbytes

We’ll go over how to use for and while loops to print a hollow square star pattern in Java. This pattern is frequently used in programming exercises and is a great way to practice nested loops and conditional statements in Java. To demonstrate how to print the hollow square pattern in Java, we will provide an algorithm, pseudocode, and Java code for both for and while loops.

What is Hollow Square Star Pattern in Java?

A hollow square pattern is one in which the square’s borders are printed with stars but the inside of the square is left blank.

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

Algorithm to Print Hollow Square Star Pattern in Java

  1. Enter the number of rows.
  2. An outer loop from 1 to N is used for rows.
  3. An inner loop from 1 to N is used for columns.
  4. Within the inner loop, print a star for the first and last row or column.
  5. Which is the print star?
  6. if i = 1 or i = N, or if j = 1 or j = N
  7. Otherwise, print space is limited.
  8. Print a blank line after the inner loop after printing all columns of a row.

Pseudo Code to Print Hollow Square Star Pattern using For Loop

The following is the pseudocode for the provided Java code:

  1. Request that the user enter the size of the square side.
  2. Create a Scanner object to read the user’s input.
  3. To see the Hollow Square Star Pattern, print the message.
  4. To iterate over each row and column of the square, use nested for loops.
  5. Determine whether the current row or column is the first or last row/column.
  6. If so, a star should be printed.
  7. If not, print two blank spaces to make a hollow square.
  8. To begin a new row, move the cursor to the next line.
  9. The program has come to an end.

Java Code to Print Hollow Square Star Pattern using For Loop

import java.util.Scanner;
public class Main 
{
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the stars in each side of square: ");
    int sideSize = sc.nextInt();

    for (int i = 0; i < sideSize; i++ )
    {
      for (int j = 0 ; j < sideSize; j++ )
      {
        if (i == 0 || i == sideSize - 1 || j == 0 || j == sideSize - 1)
        {
          System.out.print("*"+" ");
        }
        else {
          System.out.print(" "+ " ");
        }
      }
      System.out.println();
    }
  }
}

Output

Enter the stars in each side of square:
4
* * * * 
*     * 
*     * 
* * * * 

Pseudo Code to Print Hollow Square Star Pattern using While Loop

The following is the pseudocode for the provided Java code:

  1. Begin the program.
  2. Declare and initialize the variables i, j of integer type, and sideSize of integer type.
  3. Make a new Scanner object sc to read user input.
  4. Request that the user enter the number of stars on each side of the square.
  5. Take in the data and store it in the variable sideSize.
  6. Steps 7-11 should be repeated as long as i is less than sideSize.
  7. Set j to 0.
  8. Steps 9-13 should be repeated as long as j is less than sideSize.
  9. If i equals 0 or i equals sideSize – 1, or j equals 0 or j equals sideSize – 1, then print "* "
  10. Otherwise, print " "
  11. Increase j by 1
  12. Inner loop’s conclusion
  13. Create a new line of text.
  14. Increase i by 1.
  15. The end of the outer loop
  16. The program has come to an end.

Java Code to Print Hollow Square Star Pattern using While Loop

import java.util.Scanner;

public class Main{
  public static void main(String[] args) {

    int i=0, j;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the stars in each side of square: ");
    int sideSize = sc.nextInt();

    while (i < sideSize )
    {
      j = 0 ;
      while ( j < sideSize )
      {
        if (i == 0 || i == sideSize - 1 || j == 0 || j == sideSize - 1) {
          System.out.print("*"+ " ");
        }
        else {
          System.out.print("  ");
        }
        j++;
      }
      System.out.println();
      i++;
    }
  }
}

Output

Enter the stars in each side of square:
4
* * * *
*     *
*     *
* * * *

Conclusion
In this article, we’ll look at how to print a hollow square star pattern in Java. We investigated the algorithm for printing the hollow square pattern, as well as the corresponding pseudocode and Java code. We provided examples of the patterns for loop and while loop implementations. The hollow square star pattern is a simple but useful programming concept for creating interesting shapes and designs. Developers can expand their knowledge of Java programming and apply these concepts to their own projects by understanding the algorithms and code examples presented in this article.

Frequently Asked Questions (FAQs)

Q1. In Java, how do you print a solid square?
Ans. To iterate through the column, use the inner for loop structure for(j=1; j=size; j++); inside the inner loop print star "*"; This activity will continue until the outer while loop’s condition becomes false.

Q2. What exactly is a square star?
Ans. Square star shapes have square sides and resemble stars. The Node tool or the Shape panel can be used to modify the properties of a square star shape.

Q3. In Java, what is the star shape pattern?
Ans. Star patterns are a popular Java pattern program for improving logical thinking and flow control knowledge.

Leave a Reply

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