Project Solution: Creating a Hollow Square with Diagonal Lines

Rumman Ansari   Software Engineer   2024-07-06 03:57:31   60  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:


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
************
**        **
* *      * *
*  *    *  *
*   *  *   *
*    **    *
*    **    *
*   *  *   *
*  *    *  *
* *      * *
**        **
************
Creating a Hollow Square with Diagonal Lines
Figure: Creating a Hollow Square with Diagonal Lines