Java Program to Print Hollow Rectangle Pattern of Stars

star pattern 3

In this tutorial, we will write a program to print hollow rectangle pattern in java. Before that, you may go through the following topic in java.

The program takes a user input for the number of rows and columns for the hollow rectangle. Then using two for loops it will print the star rectangular pattern in java.

Example:

Inputs:
      Rows: 6
      Columns:10
Output:
**********
*        *
*        *
*        *
*        *
**********

Java Program to Print Rectangle Star Pattern

import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    int rows, columns, i, j;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the no. of rows: ");
    rows = sc.nextInt();
    System.out.print("Enter the no. of columns: ");
    columns = sc.nextInt();

    System.out.print("Rectangular star Pattern: \n\n");
    for (i = 1; i <= rows; i++)
    {
      for (j = 1; j <= columns; j++)
      {
        if (i == 1 || i == rows || j == 1 || j == columns)
          System.out.print("*");
        else
          System.out.print(" ");
      }

      System.out.println();
    }
  }
}

Output:

Hollow rectangle star pattern in java