Signup/Sign In

Java Program to Find the Area of a Rhombus

In this tutorial, we will learn how to calculate the area of a rhombus in java. A rhombus is a quadrilateral whose four sides all have the same length. The area of a rhombus can be defined as the amount of space enclosed by a rhombus in a two-dimensional space. But before moving forward if you are not familiar with the concept of data types, then do check the article on Data types in Java.

Input: Enter the first diagonal: 6

Enter the second diagonal: 4

Output: Area of the rhombus: 24

Below is the pictorial representation of the same.

Program 1: Java Program to Find the Area of a Rhombus

In this program, we will learn how to find the area of a rhombus using the base and height formula.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the value of the base and height of the rhombus.
  4. Ask the user to initialize the variables.
  5. Declare another variable to store the area of the rhombus.
  6. Use the base and height formula to calculate the area.
  7. Display the result.
  8. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Calculate the Area of a Rhombus
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user 
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the base of the rhombus: ");
        double base=sc.nextDouble();
        System.out.println("Enter the height of the rhombus: ");
        double height=sc.nextDouble();
       if (base <= 0 || height <= 0)
            System.out.println("Length should be positve");
        else
            System.out.println("Area of rhombus = "+ (base * height));
     }
}


Enter the base of the rhombus: 3
Enter the height of the rhombus: 4
Area of rhombus = 12.0

Program 2: Java Program to Find the Area of a Rhombus

In this program, we will learn how to find the area of a rhombus using diagonals.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the value of the diagonals of the rhombus.
  4. Ask the user to initialize these variables.
  5. Declare another variable to store the angle between any two diagonals of the rhombus
  6. Ask the user to initialize the variable.
  7. Conver it into radians.
  8. Calculate the sine value of the angle.
  9. Use the diagonal formula to calculate the area of the rhombus,
  10. Print the value of the area of the rhombus.
  11. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Calculate the Area of a Rhombus
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user 
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first diagonal of the rhombus: ");
        double d1=sc.nextDouble();
        System.out.println("Enter the second diagonal of the rhombus: ");
        double d2=sc.nextDouble();
       if (d1 <= 0 || d2 <= 0)
            System.out.println("Length should be positve");
        else
            System.out.println("Area of rhombus = "+ (d1 * d2) / 2);
     }
}


Enter the first diagonal of the rhombus: 30
Enter the second diagonal of the rhombus: 40
Area of rhombus = 600.0

Program 3: Java Program to Find the Area of a Rhombus

In this program, we will learn how to find the area of a rhombus using trigonometry.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a variable to store the value of the side of the rhombus.
  4. Ask the user to initialize the variables.
  5. Declare another variable to store the angle between the sides of the rhombus.
  6. Ask the user to initialize the angle.
  7. Conver it into radians.
  8. Calculate the sine value of the angle.
  9. Use the trigonometric formula to calculate the area of the rhombus.
  10. Print the value of the area of the rhombus.
  11. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Calculate the Area of a Rhombus
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user 
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the length of any side of the rhombus: ");
        double side=sc.nextDouble();
        System.out.println("Enter any interior angle: ");
        double a = sc.nextDouble();
        // converting values to radians
        double b = Math.toRadians(a);
        double area=side*side*(Math.sin(b));
       if (side <= 0)
            System.out.println("Length should be positve");
        else
            System.out.println("Area of rhombus = "+ area);
     }
}


Enter the length of any side of the rhombus: 2
Enter any interior angle: 30
Area of rhombus = 1.9999999999999998



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.