This is the Java Program to Find the Area of a Rhombus.
Problem Description
Given the dimensions of a rhombus, find out its area.
Problem Solution
The area of a rhombus can be calculated using the formula:
Area = (diagonal1)*(diagonal2)/2.
Program/Source Code
Here is the source code of the Java Program to Find the Area of a Rhombus. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Find the Area of a Rhombus
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AreaOfARhombus {
// Function to calculate the area of a rhombus
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double l1,l2;
System.out.println("Enter the length of the diagonals of the rhombus");
try{
l1=Double.parseDouble(br.readLine());
l2=Double.parseDouble(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
if(l1<=0 || l2<=0){
System.out.println("Wrong input");
return;
}
System.out.println("Area = " + (l1*l2)/2 );
}
}
Program Explanation
1. In function main(), first, the length of the diagonals of the rhombus is taken as input.
2. The condition if(l1<=0 || l2<=0) checks if the input is valid or not.
3. The print statement displays the area of the rhombus.
advertisement
Time Complexity: O(1)
Runtime Test Cases
Case 1 (Simple Test Case): Enter the length of the diagonals of the rhombus 23 45 Area = 517.5
Sanfoundry Global Education & Learning Series – Java Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Related Posts:
- Check Java Books
- Practice BCA MCQs
- Practice Information Technology MCQs
- Practice Programming MCQs
- Apply for Computer Science Internship