Hollow Rectangle Pattern | Write a program in java to print a hollow rectangle. | ISC Computer Science | ICSE Computer Application
Algorithm
- Start
- rows:=0
- columns:=0
- Print "Enter the number of rows"
- Read rows
- Print "Enter the number of columns"
- Read columns
- i=0
- if i<rows, go to step 10 or else go to step 18
- j=0
- If j<columns, go to step 12 or else go to step 16
- If i==0||i==rows-1||j==0||j==columns, go to step 13 or else go to step 14
- Print "*". Go to step 15
- Print " "
- j++. Go to step 11
- Go to next line.
- i++. Go to step 9
- Stop
Program
Buffered Reader
import java.io.*;
class HollowRectangle
{
int rows, columns;
void accept() throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the number of rows");
rows=Integer.parseInt(br.readLine());
System.out.println("Enter the number of columns");
columns = Integer.parseInt(br.readLine());
}
void displayrec()
{
for(int i=0; i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if(i==0||i==rows-1||j==0||j==columns-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
void main() throws IOException
{
accept();
displayrec();
}
}
Scanner
import java.util.*;
class HollowRectangle
{
int rows, columns;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows");
rows=sc.nextInt();
System.out.println("Enter the number of columns");
columns = sc.nextInt();
}
void displayrec()
{
for(int i=0; i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if(i==0||i==rows-1||j==0||j==columns-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
void main()
{
accept();
displayrec();
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | rows | To store the number of rows |
int | columns | To store the number of columns |
int | i | To act as a counter variable |
int | j | To act as a counter variable |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user |
void | displayrec() | To display the hollow rectangle |
void | main() | To act as an entry gateway for the compiler. |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @IscJavaCode
Twitter: @IscCode
Quora: ISC Java Code
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment