Java Program for Hollow Square Star Pattern

Printing Hollow Square Star Pattern

In this program we’re going to code a java program for hollow square star pattern program. A hollow square star pattern is one with star only on its boundaries and free space on all other places.
Take a input from user, for determining the number of rows and columns, and store it in the variable named as row and col, respectively and then write a “for loop” starting from i=1 to i<=row and then take another for loop to start from j=1 to j<=col and then we’ll use a  if statement “if(i==1 or i==col) or (j==1 or j==col)” for printing the stars on the desired places.

Hollow Square Star Pattern

Algorithm:

  • Take number of rows input from the user and store it in any variable (‘row’ in this case).
  • Take number of coloum input from the user and store it in any variable (‘col’ in this case).
  • Run  a loop ‘i’ number of times to iterate through all the rows. From i=1 to i<=row. The loop should be structured as for (int i = 1; i <=row; i++).
  • Run a nested loop inside the main loop for printing stars . From j=1 to j<=col. The loop should be structured as for (int j = 1; j <= col; j++).
  • Inside the above loop print stars only if  i==1 or i==col or j=1 or j=col in all other cases print a blank space.
  • Move to the next line by printing a new line. System.out.println();.

Code in Java:

import java.util.Scanner;
public class Pattern1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter row and col">);
		int row = sc.nextInt();
		int col = sc.nextInt();
		for (int i = 1; i <=row; i++) {
			for (int j = 1; j <= col; j++) 
				if((i==1 || i==col) || (j==1 || j==col))
					System.out.print("*");
				else
					System.out.print(" ");
			
			System.out.println();
			
		}
	}
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student) 

10 comments on “Java Program for Hollow Square Star Pattern”


  • B

    class HolloWSquarePattern {
    public static void main(String[] args) {
    int row = 6;
    int column = 6;

    for(int i=1;i<=row;i++){
    for(int j=1;j1 && i1 && j<= column-1)){
    System.out.print(" ");
    }else{
    System.out.print("* ");
    }
    }
    System.out.println("");
    }

    }
    }


  • sridharan7614

    public class HollowSquarePattern {

    public static void main(String args[]) {
    if (args.length != 2) {
    System.out.println(
    “Error: Expected 2 arguments, where the first argument is rowCount and the second is columnCount”
    );
    System.exit(1);
    }

    int rowsToPrint = Integer.parseInt(args[0]);
    int columnsToPrint = Integer.parseInt(args[1]);

    for (int i = 1; i <= rowsToPrint; i++) {
    for (int j = 1; j <= columnsToPrint; j++) {
    Boolean isCeiling = i == 1;
    Boolean isFloor = i == rowsToPrint;
    Boolean isWall = j == 1 || j == columnsToPrint;

    if (isCeiling || isFloor || isWall) {
    System.out.print("*");
    } else {
    System.out.print(" ");
    }
    }
    System.out.println();
    }
    }
    }


  • Parshurama

    public class Pattern1 {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter the nimber”>);
    int n = sc.nextInt();

    for (int i = 1; i <=n; i++) {
    for (int j = 1; j <= n; j++)
    if((i==1 || i==n) || (j==1 || j==n))
    System.out.print("*");
    else
    System.out.print(" ");

    System.out.println();

    }
    }
    }


  • Nikita Modi

    import java.util.Scanner;
    public class Hollow_Square {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter row and col”);
    int row = sc.nextInt();
    int col = sc.nextInt();
    for (int i = 1; i <=row; i++) {
    for (int j = 1; j <= col; j++)
    if((i==1 || i==row) || (j==1 || j==col))
    System.out.print("*");
    else
    System.out.print(" ");

    System.out.println();

    }

    }

    }


  • Rupesh

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

    int row = 5;
    int col = 6;
    for (int i = 1; i <=row; i++) {
    for (int j = 1; j <= col; j++)
    if((i==1 || i==col) || (j==1 || j==col))
    {System.out.print("*");}
    else
    {System.out.print(" ");}

    System.out.println();

    }
    }}


  • Lalan Thakur

    Javascript
    for(let i=1;i<=4;i++)
    {
    let row="";
    for(let j=1;j<=4;j++)
    {
    if(i==1||i==4||j==1||j==4){
    row+="*";
    } else{
    row+=" ";
    }
    }
    console.log(row)
    }


  • Rajesh

    public class Pattern1 {

    public static void main(String[] args) {
    int row=5;
    int col=5;
    for (int i = 1; i <=row; i++) {
    for (int j = 1; j <= col; j++)
    if((i==1 || i==col) || (j==1 || j==col))
    System.out.print("*");
    else
    System.out.print(" ");

    System.out.println();

    }
    }
    }