Project Solution: Hollow Square Pattern

Rumman Ansari   Software Engineer   2024-07-06 04:37:02   29 Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Solution: HollowSquare.java


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 = n; i >= 1; i--) {
            for (int j = 1; j <= i - 1; j++) {
                System.out.print(" ");
            }

            for (int k = 1; k <= n; k++) {
                if (i == n || i == 1 || k == 1 || k == n) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }

            System.out.println();
        }

        scanner.close();
    }
}


Enter the number of rows: 5
    *****
   *   *
  *   *
 *   *
*****
Hollow Square Pattern
Figure: Hollow Square Pattern

inside star


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=n;i>=1;i--)
		{
			for (int j=1;j<=i-1;j++)
			{
		 		System.out.print(" ");
			}

			for(int k=1;k<=n;k++) {
				System.out.print("*");
			}

		 System.out.print("\n");

		}
    }
}

Output:


Enter the number of rows: 5
    *****
   *****
  *****
 *****
*****