Project Question: Hollow Square Pattern
☰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.
Instructions:
-
Input:
- Prompt the user to enter the number of rows (
n
) for the square pattern. - Read and store this input using a
Scanner
.
- Prompt the user to enter the number of rows (
-
Output:
- Generate a square pattern using nested loops:
- The outer loop (
i
) controls the rows of the pattern fromn
to1
. - The first inner loop (
j
) prints spaces ( - 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
ori == 1
) or the first and last columns (k == 1
ork == n
). - Otherwise, print spaces (
- The outer loop (
- Generate a square pattern using nested loops:
-
Example Output:
- For
n = 5
:
- For
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.