The area and perimeter of a rectangle can be calculated using the formula Area = length * width and Perimeter = 2 * (length + width). Here is the program to implement this in Java. First, we take length and breadth from the user as input and then give area and perimeter as output.
Area of Rectangle
import java.util.Scanner;
public class RectangleAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rectangle length: ");
double length = scanner.nextDouble();
System.out.print("Enter rectangle width: ");
double width = scanner.nextDouble();
scanner.close();
double area = length * width;
System.out.println("Area: " + area);
}
}
Output:
Enter rectangle length: 10
Enter rectangle width: 5
Area: 50.0
Perimeter of Rectangle
import java.util.Scanner;
public class RectanglePerimeterCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rectangle length: ");
double length = scanner.nextDouble();
System.out.print("Enter rectangle width ");
double width = scanner.nextDouble();
scanner.close();
double perimeter = 2 * (length + width);
System.out.println("Perimeter: " + perimeter);
}
}
Output:
Enter rectangle length: 10
Enter rectangle width: 5
Perimeter: 30.0