Table of Contents
Project Solution: Creating a Hollow Square with Diagonal Lines
import java.util.Scanner;
public class HollowSquare {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
System.out.print("Enter the number of rows: ");
n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
Enter the number of rows: 12
************
** **
* * * *
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
* * * *
** **
************