A Program to find area and perimeter of Rectangle

If you are new to java programming language then this tutorial will be helpful in understanding the program.

A Program to find area and perimeter of Rectangle

If you are new to java programming language then this tutorial will be helpful in understanding the program.

A Program to find area and perimeter of Rectangle

A Program to find area and perimeter of Rectangle

In this section we will explain you with the help of a program to find the area and perimeter of  a rectangle. To find area and perimeter of a rectangle is a simple program in java. If you are new to java programming language then the example will help you to understand the concepts. We will write a program, which calculate the area and perimeter of a rectangle in java.

Code Description : Using this example,  first of all create a class Program. Now define two integer variable length and breadth and initializing it. As the program is based on the input provide by user, so it is advisable that always try to enter correct input otherwise it may cause exception in the program. Now create a BufferedReader class object. The InputStreamReader read the input and stored it in buffer. The input provided is of  string type for which we have to convert it into integer type by using Integer.parseInt() method. Creating a object of  the class, by using an object only we can call  the function or making function as static we can call directly it by class name. So, through the object with the dot operator, invoking the method area(), which calculate the area and perimeter and  print it to the console. Now here is your program compile and run the program and provide the correct input, so that you will get the perfect result.

Program using function to find the area and perimeter of rectangle as follows:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Program 
  {
	 static int length=0;
	 static int breadth=0;
	public static void main(String args[]) 
	  {
             try
           {
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             System.out.println(" ");
             System.out.println("Please enter length of a rectangle");
             length = Integer.parseInt(br.readLine());

             System.out.println("Please enter breadth of a rectangle");
              breadth = Integer.parseInt(br.readLine());
              Program ob=new Program();
              ob.area();
           }
        
         catch(NumberFormatException ob)
         {
                 System.out.println("Invalid input" + ob);
                 System.exit(0);
         } catch (IOException e) {
			
	               e.printStackTrace();
		     }
           
      }
	public void area()
      {
 	int perimeter = 2 * (length + breadth);
        int area = length*breadth;
        System.out.println("Perimeter of a rectangle is = " + perimeter);
        System.out.println("Area of a rectangle is = " + area);  
      } 
   }

Output from the program :

Download Source Code