🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
A hollow square pattern is a square made of stars (*) where only the boundary (i.e., the first row, last row, first column, and last column) is filled with stars, while the inner part of the square is empty (filled with spaces). This guide will walk you through writing a Java program to print a hollow square pattern of a given size.
Problem Statement
Create a Java program that:
- Accepts the size (side length) of the square.
- Prints a hollow square using stars (
*), where the boundary elements are stars, and the inside is hollow (filled with spaces).
Example:
Input:
5Output:
***** * * * * * * *****Input:
7Output:
******* * * * * * * * * * * *******
Solution Steps
- Take Input: Define the size of the hollow square.
- Print the Hollow Square Pattern: Use nested loops to print stars (
*) for the boundary and spaces () for the inside. - Display the Result: Print the hollow square pattern on the console.
Java Program
// Java Program to Print a Hollow Square Pattern
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class HollowSquare {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the square
System.out.print("Enter the size of the square: ");
int size = scanner.nextInt();
// Step 2: Print the hollow square pattern
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
// Step 3: Print '*' for the boundary, otherwise print ' '
if (i == 1 || i == size || j == 1 || j == size) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
// Move to the next line after printing each row
System.out.println();
}
// Close the scanner
scanner.close();
}
}
Explanation
Step 1: Take Input
- The program prompts the user to enter the size of the hollow square. This size determines the side length of the square.
Step 2-3: Print the Hollow Square Pattern
- A nested loop structure is used where the outer loop (
i) represents the rows, and the inner loop (j) represents the columns. - Stars (
*) are printed for boundary positions: the first row (i == 1), the last row (i == size), the first column (j == 1), and the last column (j == size). - For all other positions, spaces (
" ") are printed to create a hollow effect.
Output Example
For an input of 5, the program prints:
*****
* *
* *
* *
*****
For an input of 7, the program prints:
*******
* *
* *
* *
* *
* *
*******
Example with Different Input
For an input of 4, the program outputs:
****
* *
* *
****
Conclusion
This Java program demonstrates how to print a hollow square pattern using nested loops. By carefully controlling the placement of stars and spaces based on the row and column indices, the program creates the desired hollow square shape. This exercise is useful for practicing loop control and pattern generation in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment