star pattern

Program to print Hollow rectangle and square star pattern

Program to Print Hollow rectangle or square star pattern

In this article, we will discuss the concept of  Program to Print Hollow rectangle or square star pattern in Java programming language

In this post, we are going to learn How to write a Java program to print  Hollow rectangle or square star pattern using for loop, while loop and Do-while loops with example programs

Hollow pattern

Program 1

Print Hollow rectangle or square star pattern

Using for loop

This program allows the user  to enter the number of rows and columns and then it will display Hollow rectangle and square pattern using for loop in Java language

import java.util.Scanner;
class Hollow_RectanglePat{
public static void main(String args[]){
int rows,columns;//declare variable size
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
System.out.print("Enter the nunber of rows: ");
rows=scan.nextInt();
//get input from the user for row

System.out.print("Enter the nunber of rows: ");
columns=scan.nextInt();
//get input from the user for coloum


for (int i=1; i<=rows; i++){
    for (int j=1; j<=columns; j++){
    if(i==1||i==rows||j==1||j==columns){
        System.out.print("*");
    }else{
        System.out.print(" ");
    }
    
}
    System.out.print("\n");
}
}
}

When the above code is executed, it produces the following results

Rectavle and square star pattern

 

Program 2

Using while loop

This program allows the user  to enter the number of rows and columns and then it will display Hollow rectangle and square pattern using while  loop in Java language

import java.util.Scanner;
class Hollow_RectanglePat1{
public static void main(String args[]){
int rows,columns;//declare variable size
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
System.out.print("Enter the nunber of rows: ");
rows=scan.nextInt();
//get input from the user for row

System.out.print("Enter the nunber of rows: ");
columns=scan.nextInt();
//get input from the user for coloum

int i=1;
while( i<=rows){
    int j=1;
    while( j<=columns){
    if(i==1||i==rows||j==1||j==columns){
        System.out.print("*");
    }else{
        System.out.print(" ");
    }
     j++;
    
}
    System.out.print("\n");
    i++;
}
}
}

When the above code is executed, it produces the following results

Rectangle and square star pattern

 

Program 3

Using the do-while loop

This program allows the user  to enter the number of rows and columns and then it will display Hollow rectangle and square pattern using the do-while loop in Java language

import java.util.Scanner;
class Hollow_RectanglePat2{
public static void main(String args[]){
int rows,columns;//declare variable size
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
System.out.print("Enter the nunber of rows: ");
rows=scan.nextInt();
//get input from the user for row

System.out.print("Enter the nunber of rows: ");
columns=scan.nextInt();
//get input from the user for coloum

int i=1;
do{
    int j=1;
    do{
    if(i==1||i==rows||j==1||j==columns){
        System.out.print("*");
    }else{
        System.out.print(" ");
    }
     j++;
    
}while( j<=columns);
    System.out.print("\n");
    i++;
}while( i<=rows);
}
}

When the above code is executed, it produces the following results

Rectangle and square star pattern

 

Suggested for you

if statement in Java language

For loop in Java language

Nested For loop in Java language

while loop in Java language

Nested while  loop in Java language

Do-while loop in Java language

Nested Do-while loop in Java language

The operator in Java language

Datatype and variable in Java language

 

Similar post

C  code to Print Hollow rectangle and square star pattern

C++ code to Print Hollow rectangle and square star pattern

C  code to display Hollow Pyramid star pattern

Java code to display Hollow Pyramid and square star pattern

C++  code to display Hollow Pyramid and square star pattern

 

 

Print Hollow rectangle and square star pattern in C
C++ program for rectangle and square star pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago