Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Display Hollow Rectangle star pattern

Program to Display Hollow square star pattern in Java using loops

Posted on October 26, 2019September 28, 2020

Table of Contents

  • Program to Display Hollow square star pattern in Java using loops
    • Program to Display Hollow square star pattern using for loop
    • Program to Display Hollow square star pattern using while loop
    • Program to Display Hollow square star pattern using the do-while loop

Program to Display Hollow square star pattern in Java using loops

In this tutorial, we will discuss the program to display Hollow square star pattern in Java using loops

We can display many types of number, Star, Alphabet patterns using for, while and do-while loop in Java language

In this post, we are going to learn how to display Hollow square pattern  Using for, while and do-while loop in Java language

Program to Display Hollow square star pattern using for loop

This program allows the user to enter the size of the rectangle and then it displays Hollow aquare star pattern using for loop in Java language

Program 1

import java.util.Scanner;
class Hollow_Rectangle{
public static void main(String args[]){
int size;//declare variable size
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
System.out.print("Enter the integer number: ");
size=scan.nextInt();//get input from the user for num1
for (int row=1; row<=size; row++){
  System.out.print("*");
}
System.out.print("\n");
for (int coloum=1; coloum<=size-2; coloum++){
  for (int row=1; row<=size; row++){
  if(row==1||row==size){
    System.out.print("*");
  }else{
    System.out.print(" ");
  }
  
}
  System.out.print("\n");
}
for(int row=1; row<=size; row++){
  System.out.print("*");
}
System.out.print("\n");
}
}
When the above code is executed it produces the following output
Program to Display Hollow square star pattern in Java using loops
Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the first for loop from 1 to given size according to the loop structure for (row=1; row<=size; row++) to print star for first-line
  • To iterate through the column, run the outer for loop from 1 to given size according to the loop structure for (coloum=1; coloum<=size-2; coloum++)
  • Inner for loop print star when the if statement is satisfied if(row==1||row==size) otherwise print space
  • This activity continues until the condition of outer while loop becomes false
  • Finally, run the last for loop from 1 to given size according to the loop structure for(row=1; row<=size; row++) to print star for the last line

Program to Display Hollow square star pattern using while loop

Program 2
This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using while loop in Java language
import java.util.Scanner;
class Hollow_Rectangle1{
public static void main(String args[]){
int size;//declare variable size
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
System.out.print("Enter the integer number: ");
size=scan.nextInt();
//get input from the user for num1
int row=1;
while( row<=size){
  System.out.print("*");
  row++;
}
System.out.print("\n");
int coloum=1; 
while(coloum<=size-2){
  row=1;
  while( row<=size){
  if(row==1||row==size){
    System.out.print("*");
  }else{
    System.out.print(" ");
  }
  row++;
}
  System.out.print("\n");
  coloum++;
}
row=1;
while(row<=size){
  System.out.print("*");
  row++;
}
System.out.print("\n");
}
}

When the above code is executed it produces the following output

Program to Display Hollow square star pattern in Java using loops
Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the first for loop from 1 to given size according to the loop structure while( row<=size) to print star for first-line
  • To iterate through the column, run the outer for loop from 1 to given size according to the loop structure while(coloum<=size-2)
  • Inner while loop print star when the if statements satisfied if(row==1||row==size) otherwise print space
  • This activity continues until the condition of outer while loop becomes false
  • Finally, run the last for loop from 1 to given size according to the loop structure while( row<=size) to print star for the last line

Program to Display Hollow square star pattern using the do-while loop

Program 3
This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using the do-while loop in Java language
import java.util.Scanner;
class Hollow_Rectangle2{
public static void main(String args[]){
int size;//declare variable size
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
System.out.print("Enter the integer number: ");
size=scan.nextInt();
//get input from the user for num1
int row=1;
do{
  System.out.print("*");
  row++;
}while( row<=size);
System.out.print("\n");
int coloum=1; 
do{
  row=1;
  do{
  if(row==1||row==size){
    System.out.print("*");
  }else{
    System.out.print(" ");
  }
  row++;
}while( row<=size);
  System.out.print("\n");
  coloum++;
}while(coloum<=size-2);
row=1;
do{
  System.out.print("*");
  row++;
}while(row<=size);
System.out.print("\n");
}
}

When the above code is executed it produces the following output

Program to Display Hollow square star pattern in Java using loops
Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the first do-while loop from 1 to given size according to the loop structure while( row<=size) to print star for the first line
  • To iterate through the column, run the outer dowhile loop from 1 to given size according to the loop structure while(coloum<=size-2)
  • Inner do-while loop print star when the if statements satisfied if(row==1||row==size) otherwise print space
  • This activity continues until the condition of outer while loop becomes false
  • Finally run the last do-while loop from 1 to given size according to the loop structure while( row<=size) to print star for the last line

 

Suggested for you
for loop in Java language
while loop in Java language
do-while in Java language
Operator in Java language
Data type in Java language

 

Similar post
Display Hollow rectangular pattern in C using loops
Display Hollow rectangular pattern in C++ using loops

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes