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:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the base and height of the rhombus.
- Ask the user to initialize the variables.
- Declare another variable to store the area of the rhombus.
- Use the base and height formula to calculate the area.
- Display the result.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the diagonals of the rhombus.
- Ask the user to initialize these variables.
- Declare another variable to store the angle between any two diagonals of the rhombus
- Ask the user to initialize the variable.
- Conver it into radians.
- Calculate the sine value of the angle.
- Use the diagonal formula to calculate the area of the rhombus,
- Print the value of the area of the rhombus.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the value of the side of the rhombus.
- Ask the user to initialize the variables.
- Declare another variable to store the angle between the sides of the rhombus.
- Ask the user to initialize the angle.
- Conver it into radians.
- Calculate the sine value of the angle.
- Use the trigonometric formula to calculate the area of the rhombus.
- Print the value of the area of the rhombus.
- 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