Find Area Of Rectangle Java Program

Sharing is healthy !!

In this program you will to find area of rectangle java program.

In this program user will be prompted to enter the length and width of a rectangle.

We will be using three classes namely BufferedReader, IOException and InputStreamReader of java.io package.

So let’s get into code implementation,

Find Area Of Rectangle Java Program

example: Find Area Of Rectangle Java Program

Also Read – Area of Circle Java Program

/* Area of rectangle is, Area = length * width. */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RectangleArea {

public static void main(String[] args) {
int width = 0;
int length = 0;

try
{
// reading the length from console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter length of a rectangle: ");
length = Integer.parseInt(br.readLine());

// reading the width from console
System.out.println("Enter width of a rectangle: ");
width = Integer.parseInt(br.readLine());
}

// to check if invalid value is entered
catch(NumberFormatException nfe)
{
System.out.println("Invalid value " + nfe);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IOError: " +ioe);
System.exit(0);
}
int area = length * width;
System.out.println("Area of rectangle is " + area);
}
}

Output:

Enter length of a rectangle: 5
Enter width of a rectangle: 4
Area of rectangle is 20


finding perimeter of rectangle

We can also find the perimeter of rectangle using the formula as below

/* Perimeter of rectangle is, Perimeter = 2 * ( length + width.) */

import java.util.Scanner;

public class RectanglePerimeterDemo
{
public static void main(String[] args)
{
int length, width, perimeter;
Scanner sc = new Scanner(System.in);

System.out.println("Enter length of a rectangle: ");
length = sc.nextInt();

System.out.println("Enter length of a rectangle: ");
width = sc.nextInt(); 

perimeter = 2 * ( length + width ); 
System.out.println("The perimeter of Rectangle is : " + perimeter);

sc.close();
}
}

Output:

Enter length of a rectangle: 5
Enter width of a rectangle: 10
The perimeter of Rectangle is : 30

Find Area Of Rectangle Java Program using function

In this program I am using a function which computes the area of rectangle using two parameters length and width.

Then the value “area” is returned to the main function from where it is called.

Let’s see the code execution,

import java.util.*;
 
public class AreaRectangle
{ 
public static void main(String []args)
{
double length, width, area;
 
Scanner sc = new Scanner(System.in);
System.out.print("Enter length of a rectangle: ");
length = sc.nextDouble();
System.out.print("Enter width of a rectangle: ");
width = sc.nextDouble();
 
area = AreaOfRectangle(length, width);

System.out.println("Area of Rectangle: " + area);
sc.close();
}
 
public static double AreaOfRectangle(double length, double width)
{
double area;
area = length * width;
return area;
}
}

Output:

Enter length of a rectangle: 60
Enter width of a rectangle: 40
Area of Rectangle: 2400.0

without using scanner class and functions

We can also write java program to calculate area of rectangle without using scanner class and functions.

This is how we can accomplish,

public class AreaRectangle
{ 
public static void main(String []args)
{
double length = 60.0;
double width = 40.0;
double area, perimeter;
 
area = length * width;
System.out.print("The area of rectangle is : " + area + "\n");
 
perimeter = 2 * ( length + width );
System.out.print("The perimeter of rectangle is : " + perimeter);
} 
}

Output:

The area of rectangle is : 2400.0
The perimeter of rectangle is : 200.0

conclusion

So this is how we can write to execute area of rectangle program in java. I hope you guys have understood the concept.

You can subscribe to my blog flower brackets if you haven’t.

If you like this post share it across social networks.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.