Project Question: Hollow Square Pattern

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

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:
<span class="pln">
</span><span class="typ">Enter</span><span class="pln"> the number </span><span class="kwd">of</span><span class="pln"> rows</span><span class="pun">:</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
    </span><span class="pun">*****</span><span class="pln">
   </span><span class="pun">*</span><span class="pln">   </span><span class="pun">*</span><span class="pln">
  </span><span class="pun">*</span><span class="pln">   </span><span class="pun">*</span><span class="pln">
 </span><span class="pun">*</span><span class="pln">   </span><span class="pun">*</span><span class="pln">
</span><span class="pun">*****</span>

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.

No Questions Data Available.
No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.