Calculate Area of Triangle Using Heron's Formula in Java



Heron’s formula gives the area of a triangle when the length of all three sides are already known.

Let’s say we have the following three sides of a triangle −

s1 = 15191235.0;
s2 = 15191235.0;
s3 = 1.01235479;

Now, use the Heron’s formulae to find the area −

area = (s1+s2+s3)/2.0d;
resArea = Math.sqrt(area* (area - s1) * (area - s2) * (area - s3));

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      // sides of a triangle
      double s1, s2, s3;
      double area, resArea;
      // three sides of a triangle
      s1 = 15191235.0;
      s2 = 15191235.0;
      s3 = 1.01235479;
      area = (s1+s2+s3)/2.0d;
      resArea = Math.sqrt(area* (area - s1) * (area - s2) * (area - s3));
      System.out.println("Area of Triangle = " + resArea);
   }
}

output

Area of Triangle = 7689459.767977145
Updated on: 2019-07-30T22:30:25+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements