Project Question: Hollow Square Pattern

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

Table of Content:

Assignment: Hollow Square Pattern

Objective:

To implement a Java program that takes an integer input n from the user and prints a hollow square pattern of asterisks (*) with spaces.

Hollow Square Pattern
Figure: Hollow Square Pattern

Instructions:

  1. Input:

    • Prompt the user to enter the number of rows (n) for the square pattern.
    • Read and store this input using a Scanner.
  2. Output:

    • Generate a square pattern using nested loops:
      • The outer loop (i) controls the rows of the pattern from n to 1.
      • The first inner loop (j) prints spaces ( ) to create the left indentation based on the current row.
      • The second inner loop (k) prints asterisks (*) to form the hollow square.
      • Ensure that asterisks are printed only on the first and last rows (i == n or i == 1) or the first and last columns (k == 1 or k == n).
      • Otherwise, print spaces ( ) to create the hollow effect inside the square.
  3. Example Output:

    • For n = 5:

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

Submission:

  • Write and submit your Java program (HollowSquare.java).
  • Ensure the program follows good coding practices (comments, variable naming conventions, etc.).
  • Test your program with different values of n to ensure it produces the correct square pattern.