Print hollow rectangle pattern using ‘*’. See example for more details.
Input Format
Input contains two integers W – width of the rectangle and L – length of the rectangle.
Constraints
2 <= W <= 50
2 <= L <= 50
Output Format
For the given integers W and L, print the hollow rectangle pattern.
Input | Output |
---|---|
5 4 | ***** * * * * ***** |
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int W = sc.nextInt();
int L = sc.nextInt();
for (int i = 0; i < L; i++) {
for (int j = 0; j < W; j++) {
if (i == 0 || i == L - 1 || j == 0 || j == W - 1) {
System.out.print('*');
} else {
System.out.print(' ');
}
}
System.out.println();
}
}
}