Java Program to Find the Area of Rhombus

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.

  1.  
  2. //Java Program to Find the Area of a Rhombus
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class AreaOfARhombus {
  8.     // Function to calculate the area of a rhombus
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         double l1,l2;
  12.         System.out.println("Enter the length of the diagonals of the rhombus");
  13.         try{
  14.             l1=Double.parseDouble(br.readLine());
  15.             l2=Double.parseDouble(br.readLine());
  16.         }catch (Exception e){
  17.             System.out.println("An error occurred");
  18.             return;
  19.         }
  20.         if(l1<=0 || l2<=0){
  21.             System.out.println("Wrong input");
  22.             return;
  23.         }
  24.         System.out.println("Area = " + (l1*l2)/2 );
  25.     }
  26. }
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
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!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.