10

Java Program to Calculate Area and Perimeter of Rectangle

 11 months ago
source link: https://www.thejavaprogrammer.com/java-program-to-calculate-area-and-perimeter-of-rectangle/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java Program to Calculate Area and Perimeter of Rectangle

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK