Java Program to Find Area and Perimeter of Rectangle

  • Write a program in Java to take length and breadth of a rectangle as input from user and calculate area and perimeter of rectangle.

To calculate the perimeter and area of rectangle, we need the dimensions of two adjacent sides(length and width) of a rectangle. The area of a rectangle is the amount of two-dimensional space inside the boundary on rectangle. The perimeter of a rectangle is the linear distance around the boundary of the rectangle.


C program to find area of rectangle
Area of Rectangle
To find the area of rectangle we multiply it's Length and Width.
  • Area of Rectangle = L X W
Where,
   L is the length of longer side of rectangle
   W is the length of smaller side of rectangle
Perimeter of Rectangle
We can calculate perimeter of rectangle as follows:
  • Perimeter of Rectangle = 2 X (L + W)
Where,
   L is the length of longer side of rectangle
   W is the length of smaller side of rectangle

Java program to calculate area and perimeter of a rectangle

package com.tcc.java.programs;

import java.util.*;

public class RectangleAreaPerimeter {
    public static void main(String args[]) {
        int length, width, area, perimeter;
  
        Scanner in = new Scanner(System.in);
        System.out.println("Enter length of Rectangle");
        length = in.nextInt();
        System.out.println("Enter width of Rectangle");
        width = in.nextInt();
        // Area of rectangle = length X width
        area = length*width;
        // Perimeter of rectangle = 2 X (length X width)
        perimeter = 2*(length + width);
        System.out.println("Area of Rectangle : "+ area);
        System.out.println("Rectangle of Rectangle : "+ perimeter);
    }
}
Output
Enter length of Rectangle
4
Enter width of Rectangle
5
Area of Rectangle : 20
Rectangle of Rectangle : 18