Java Online Compiler
Example: Hollow Rectangle Star Pattern in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Hollow Rectangle Star Pattern import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt user for the number of rows System.out.print("Enter the number of rows for the hollow rectangle: "); int rows = scanner.nextInt(); // Step 2: Prompt user for the number of columns System.out.print("Enter the number of columns for the hollow rectangle: "); int cols = scanner.nextInt(); System.out.println("\nHollow Rectangle Pattern:"); // Step 3: Outer loop iterates through each row for (int i = 0; i < rows; i++) { // Step 4: Inner loop iterates through each column in the current row for (int j = 0; j < cols; j++) { // Step 5: Check if the current position is on the border // Border conditions: first row (i==0), last row (i==rows-1), // first column (j==0), last column (j==cols-1) if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) { System.out.print("*"); // Print star if on border } else { System.out.print(" "); // Print space if in the interior } } // Step 6: After printing all columns for a row, move to the next line System.out.println(); } scanner.close(); } }
Output
Clear
ADVERTISEMENTS