Hollow Rectangle Pattern | Write a program in java to print a hollow rectangle. | ISC Computer Science | ICSE Computer Application

Image is Unavailable


Algorithm

  1.   Start
  2. rows:=0
  3. columns:=0
  4. Print "Enter the number of rows"
  5. Read rows
  6. Print "Enter the number of columns"
  7. Read columns
  8. i=0
  9. if i<rows, go to step 10 or else go to step 18
  10. j=0
  11. If j<columns, go to step 12 or else go to step 16
  12. If i==0||i==rows-1||j==0||j==columns, go to step 13 or else go to step 14
  13. Print "*". Go to step 15
  14. Print " "
  15. j++. Go to step 11
  16. Go to next line.
  17. i++. Go to step 9
  18. 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 NameDescription
int rows To store the number of rows 
 intcolumns To store the number of columns 
 intTo act as a counter variable 
 intTo act as a counter variable 

Method Description

Return Type Method NameDescription
 voidaccept() To accept data from the user 
 voiddisplayrec() To display the hollow rectangle 
 voidmain() To act as an entry gateway for the compiler. 

Output

Image is Unavailable





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
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful


Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences