Hollow rectangle pattern
Table of Contents
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
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");
}
} Approach
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
Approach
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
Approach
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.