Java Program to calculate the area of a triangle using Heron's Formula

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 23-Apr-2019 12:15:28
Advertisements