Java Program to Find Area and Perimeter of Rectangle

In the previous article we have discussed Java Program to Find Perimeter of Triangle

In this article we will discuss about how to find area and perimeter of a rectangle.

Program to Find Area and Perimeter of Rectangle

Before going into the program, let’s see how we find the area and perimeter of a rectangle.

Formula for area of Rectangle = 2 * (length + breadth)

Formula for perimeter of Rectangle = length * breadth

Where,

  •  length represents the length of the rectangle. (longer side of rectangle)
  • breadth represents the breadth of the rectangle. (smaller side of rectangle)

Example:

Example- To find Area of Rectangle

When length of rectangle = 4
and breadth of rectangle = 5
Then area of rectangle => area = 2 * (length + breadth)
                                     => area = 2*(4+5)
                                     => area = 18
Example- To find Perimeter of Rectangle

When length of rectangle = 4
and breadth of rectangle = 5
Then area of rectangle => perimeter = (length * breadth)
                                     => perimeter = 4*5
                                     => perimeter =18

Now, let’s see the program.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Method-1: Java Program to Find Area and Perimeter of Rectangle By using static values

In this values for length and breadth of rectangle are already taken.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //variables declared
        double length, breadth, perimeter, area;
        //length value declared
        length = 4.2;
        // breadth value declared
        breadth = 2.0;
        //finding perimeter
        perimeter = 2 * (length + breadth);
        System.out.println("Perimeter of rectangle :"+perimeter);
        //finding area
        area = length * breadth;
        System.out.println("Area of rectangle :"+area);
    }
}
Output:

Perimeter of rectangle : 12.4
Area of rectangle : 8.4

Method-2: Java Program to Find Area and Perimeter of Rectangle By using dynamic values

In this values for length and breadth of rectangle will be taken by the user.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //variables declared
        int length, breadth, perimeter, area;
        Scanner sc = new Scanner(System.in);
        //taking length input from user
        System.out.print("Enter length of rectangle : ");
        length = sc.nextInt();
        //taking breadth input from user
        System.out.print("Enter breadth of rectangle : ");
        breadth = sc.nextInt();
        //finding perimeter
        perimeter = 2 * (length + breadth);
        System.out.println("Perimeter of rectangle : "+perimeter);
        //finding area
        area = length * breadth;
        System.out.println("Area of rectangle : "+area);
    }
}
Output:

Enter length of rectangle : 3
Enter breadth of rectangle : 4
Perimeter of rectangle : 14
Area of rectangle : 12

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: