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 fromnto1. - 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 == nori == 1) or the first and last columns (k == 1ork == n). - Otherwise, print spaces (
) to create the hollow effect inside the square.
- 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
nto ensure it produces the correct square pattern.