Find the Perimeter of a Rectangle in Java



For a given rectangle of length l and width w, write a Java program to find its perimeter. The Perimeter of a rectangle is calculated by adding the lengths of all the sides of the rectangle.

Below is a demonstration of a rectangle, a quadrilateral with four right angles (90°). The perimeter of a rectangle is the total length of the two lengths and two widths of the rectangle ?

Example Scenario:

Input: length = 5, 8, 5, 8;
Output: Perimeter = 26

On adding all the sides of rectangle, you will get perimeter. 5+8+5+8 = 26

Steps to Find Perimeter of a Rectangle in Java

Follow the steps below to calculate perimeter of a rectangle ?

  • Step 1 - START

  • Step 2 - Declare and initialize length, breadth and perimeter of the rectangle.

  • Step 3 - Calculate the perimeter by adding up all the sides or use the formula of perimeter.

  • Step 4 - Display the Perimeter value.

  • Step 5 - STOP

Example 1

The following example shows how to calculate perimeter of a rectangle in Java.

public class RectanglePerimeter {
   public static void main(String[] args) {
      // length of the rectangle
      double length = 9.0; 
      // width of the rectangle
      double width = 7.0; 
      // perimeter of the rectangle
      double perimeter = 2 * (length + width);
      System.out.println("The perimeter of rectangle is: " + perimeter);
   }
}

Output

The perimeter of rectangle is: 32.0

Example 2

This is another Java program that illustrates how to find perimeter of a rectangle.

public class RectanglePerimeter{
   public static void main (String args[]){
      float a ,b, c, d, perimeter;
      a=c= 8;
      b=d= 5;
      System.out.printf("The length of the sides of the Rectangle are %.2f %.2f %.2f %.2f", a, b, c, d);
      perimeter = a + b + c + d;
      System.out.printf("\nThe perimeter of Rectangle is: %.2f",perimeter);
   }
}

Output

The length of the sides of the Rectangle are 8.00 5.00 8.00 5.00
The perimeter of Rectangle is: 26.00
Updated on: 2024-08-01T11:03:16+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements