Hollow Rectangle Star Pattern

Program to Print Hollow Rectangle Star Pattern

PRINTING PATTERN:

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

PREREQUISITE:

Basic knowledge of Java language and loops.

ALGORITHM:

1. Take the number of rows and columns as input from the user and store them in variables ('rows' and 'cols').
2. Run a loop 'rows' times to iterate through the rows (i=0 to i<rows).
3. Inside the outer loop, check if the current row is the first or last row. If so, print '*' for all columns.
4. For the rows in between, print '*' at the beginning and end, and spaces in between.
5. Move to the next line after each row has been printed.

Code in Java:

import java.util.Scanner;

public class HollowRectanglePattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of rows for the hollow rectangle:");
        int rows = scanner.nextInt();  // Taking input for number of rows
        System.out.println("Enter the number of columns for the hollow rectangle:");
        int cols = scanner.nextInt();  // Taking input for number of columns

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                // Print '*' for the first and last row, and first and last column
                if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");  // Print space for hollow part
                }
            }
            System.out.println();  // New line after each row
        }

        scanner.close();
    }
}
            

Output:

Enter the number of rows for the hollow rectangle:
4
Enter the number of columns for the hollow rectangle:
6
******
*    *
*    *
******