Java Program to Find Area, Perimeter, and Side of a Rhombus

Java Program to Find Area, Perimeter, and Side of a Rhombus

Java Program to Find Area, Perimeter, and Side of a Rhombus


Introduction

A rhombus is a special type of quadrilateral where all four sides are of equal length, and opposite angles are equal. It is also known as a diamond in common usage. In geometry, understanding the properties of a rhombus is essential, and calculating its area, perimeter, and side length is a common task.

This program demonstrates how to use Java to compute these properties of a rhombus. With the lengths of its diagonals provided, the area, perimeter, and side length can be efficiently calculated.


Formulas

To calculate the properties of a rhombus, the following formulas are used:

  • Area: The area of a rhombus is calculated using the lengths of its diagonals (d1 and d2). The formula is: Area = (d1 × d2) / 2.
  • Perimeter: Since all sides of a rhombus are equal, the perimeter is given by: Perimeter = 4 × side.
  • Side Length: The side length can be derived using the Pythagorean theorem. If the diagonals are d1 and d2, the side length is: Side = √((d1/2)² + (d2/2)²).

Code

The following Java program calculates the area, perimeter, and side length of a rhombus:

import java.util.Scanner;

public class RhombusCalculations {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input the lengths of the diagonals
        System.out.print("Enter the length of the first diagonal (d1): ");
        double d1 = scanner.nextDouble();

        System.out.print("Enter the length of the second diagonal (d2): ");
        double d2 = scanner.nextDouble();

        // Calculate the area of the rhombus
        double area = (d1 * d2) / 2;

        // Calculate the side length of the rhombus
        double halfD1 = d1 / 2;
        double halfD2 = d2 / 2;
        double side = Math.sqrt((halfD1 * halfD1) + (halfD2 * halfD2));

        // Calculate the perimeter of the rhombus
        double perimeter = 4 * side;

        // Display the results
        System.out.println("Area of the rhombus: " + area + " square units");
        System.out.println("Side length of the rhombus: " + side + " units");
        System.out.println("Perimeter of the rhombus: " + perimeter + " units");

        scanner.close();
    }
}
        

Output

Below is an example of the program's output when executed with sample inputs:

Enter the length of the first diagonal (d1): 12
Enter the length of the second diagonal (d2): 10
Area of the rhombus: 60.0 square units
Side length of the rhombus: 7.81025 units
Perimeter of the rhombus: 31.241 units
        

Explanation

The program uses the Scanner class to read input from the user, specifically the lengths of the diagonals. These values are then used in the following calculations:

  • Area: The program computes the area using the formula (d1 × d2) / 2.
  • Side Length: To find the side length, the program divides each diagonal by 2 and applies the Pythagorean theorem: √((d1/2)² + (d2/2)²).
  • Perimeter: Using the side length, the program multiplies it by 4 to calculate the perimeter.

Each result is then displayed to the user, providing a complete overview of the rhombus's geometric properties.