Java Program to Find the Area of a Triangle

«
»

This is the Java Program to Find the Area of a Triangle Using Heron’s formula.

Problem Description

Write a Java Program to Find the Area of a Triangle Using Heron’s formula.

Problem Solution


The area of a triangle can be calulated as (s*(s-a)*(s-b)*(s-c))^(1/2), where s is semi-perimeter of the triangle
s = (a + b + c)/2
.

Program/Source Code

Here is the source code of the Java Program to Find the Area of a Triangle Using Heron’s formula. 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 Triangle Using Heron's formula
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class HeronsFormula {
  8.     // Function to calculate area using Heron's Formula
  9.     static void area(double a, double b, double c){
  10.         double s = (a+b+c)/2;
  11.         s = s*(s-a)*(s-b)*(s-c);
  12.         System.out.println("Area of the triangle is " + Math.sqrt(s));
  13.     }
  14.     // Function to read user input
  15.     public static void main(String[] args) {
  16.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.         double a,b,c;
  18.         System.out.println("Enter the three sides of the triangle");
  19.         try{
  20.             a = Double.parseDouble(br.readLine());
  21.             b = Double.parseDouble(br.readLine());
  22.             c = Double.parseDouble(br.readLine());
  23.         }catch (Exception e){
  24.             System.out.println("An error occurred");
  25.             return;
  26.         }
  27.         if(a<0 || b<0 || c<0){
  28.             System.out.println("Invalid Input");
  29.             return;
  30.         }
  31.         area(a,b,c);
  32.     }
  33. }
Program Explanation

1. In function area(), firstly the semi-perimeter is calculated.
2. Then, the area is calculated using heron’s formula and displayed.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Time Complexity: O(1).

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the three sides of the triangle
14
56
43
Area of the triangle is 127.31236192923294

Sanfoundry Global Education & Learning Series – Java Programs..

Check this: Programming Books | BCA MCQs

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 & technical discussions at Telegram SanfoundryClasses.