Write a Java Program to find the Perimeter of a Rectangle with an example. This example allows entering rectangle width and height, and the addition of both multiplies by two gives the 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 Width = "); width = sc.nextDouble(); System.out.print("Enter the Height = "); height = sc.nextDouble(); Perimeter = 2 * (width + height); System.out.format("The Perimeter = %.2f", Perimeter); } }
Enter the Width = 17
Enter the Height = 44
The Perimeter = 122.00
In this 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); } }
