Java Program to find Perimeter of a Rectangle

Write a Java Program to find the Perimeter of a Rectangle with an example. This Java example allows entering rectangle width and height. The addition of both multiplies by two gives the rectangle area. Thus, the Perimeter of a Rectangle equals 2 * (width + height).

package Area;

import java.util.Scanner;

public class PerimOfRectangle1 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		double width, height, Perimeter; 
		
		System.out.print("Enter the Rectangle Width =  ");
		width = sc.nextDouble();
		
		System.out.print("Enter the Rectangle Height = ");
		height = sc.nextDouble();

		Perimeter = 2 * (width + height);

		System.out.format("The Perimeter of a Rectangle = %.2f", Perimeter);
	}
}
Enter the Rectangle Width =  17
Enter the Rectangle Height = 44
The Perimeter of a Rectangle = 122.00

In this Java program, we declared a rectanglePerimeter function that returns the rectangle perimeter.

package Area;

import java.util.Scanner;

public class PerimOfRectangle2 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		float width, height, Perimeter; 
		
		System.out.print("Enter the Rectangle Width =  ");
		width = sc.nextFloat();
		
		System.out.print("Enter the Rectangle Height = ");
		height = sc.nextFloat();

		Perimeter = rectanglePerimeter(width, height);

		System.out.format("The Perimeter of a Rectangle = %.2f", Perimeter);
	}
	
	public static float rectanglePerimeter(float width, float height) {
		return 2 * (width + height);
	}
}
Java Program to find Perimeter of a Rectangle 1