BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Print Hollow Square Star Pattern

Last Updated: June 26, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this article, you will learn how to write a Java program to print hollow square star pattern.

Hollow Square star pattern illustration:

Number of rows: 5
Output:
* * * * * 
*       * 
*       * 
*       * 
* * * * *

Example 1: Print hollow square star pattern using for loop

In this example, we are using for loop to print the hollow square pattern based on the user input. User enters the max stars in each row, these stars represents the size of the each side of the square. Here user enters number 5 which means each side of the square consists of 5 stars in the output.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

    //Getting the size of the square side from user
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the stars in each side of square: ");
    int sideSize = sc.nextInt();

    System.out.println("Hollow Square Star Pattern: ");

    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 {
          // Double spaces are to accommodate space between the stars
          // For example, when square side size is 5, the total spaces
          // in 2nd row, between  first and last star are:
          // 7 (3 for stars + 4 for spaces between stars)
          System.out.print(" "+ " ");
        }
      }
      //To move the cursor to new line
      System.out.println();
    }
  }
}

Output:

Print hollow square star pattern in java

Example 2: Print hollow square star pattern using while loop

In the following example, we are using while loop to print the pattern. This is same as the first example except here we are using the same logic using while loop.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

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

    System.out.println("Hollow Square Star Pattern: ");

    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++;
      }
      //To move cursor to new line for next row
      System.out.println();
      i++;
    }
  }
}

Output:

Print hollow square star pattern using while loop in java

Recommended Java Programs:

  1. Java program to print sandglass star pattern
  2. Java program to print left pascal triangle pattern
  3. Java program to print right pascal triangle pattern
  4. Java program to print right down mirror star pattern
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Program to Implement Merge Sort in Java
  2. Java Program to remove duplicate elements in an Array
  3. Java Program to Print Pyramid Star Pattern
  4. Java Program to Print Left Pascal Triangle Pattern
  5. Java Program to Print Right Pascal’s Triangle Pattern

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap