Skip to content

Hollow Rectangle

Print a rectangle with only the border filled.

package DSA.Patterns;
import java.util.Scanner;
public class hollowRectangle {
public static void rectangle(int a, int b) {
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
if (i == 1 || i == a || j == 1 || j == b) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter Rows and Colum numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
rectangle(a, b);
} catch (Exception e) {
System.out.println(e);
}
}
}