Java Program to calculate Area of Triangle using Heron’s formula

Hello coders, in this post, we will learn to write Java program to calculate area of triangle using Heron’s formula. In the previous tutorial, you have learned Java program to calculate area of a triangle, but today we will learn to calculate area of triangle using Heron’s formula in Java.

Heron’s formula is as follows:

Let us assume the sides of a triangle are A, B, and C.

To calculate the area of the triangle using Heron’s formula, we first need to find the semi-perimeter of the triangle using the following formula.

Semi-perimeter of triangle (S) = (A+B+C)/2

Now the area of triangle can be find using following formula.

Area of triangle = √ S(S-A)(S-B)(S-C)

Now let us write the program.

Java Program to calculate Area of Triangle using Heron’s formula

What we did ?

  • First of all we imported util package.
  • Then we have created a public class named as AreaOfTriangle.
  • Now inside AreaOfTriangle class we created main() method.
  • Then we have declared variables a, b, and c for three sides of triangle, S for semi-perimeter and area for area.
  • Now we created object of the Scanner class.
  • Then asked the user to enter three sides of the triangle.
  • In next line read the value of three sides of triangle entered by the user.
  • Then calculated semi perimeter of the triangle using formula S = (a + b + c)/2.
  • In the next line calculated area of triangle using Heron’s formula area = Math.sqrt(S * (S – a) * (S – b) * (S – c))
  • Then at last printed area of triangle on screen.

Output

So guys, this was all from this tutorial on Java program to calculate area of triangle using Heron’s formula. In the next tutorial, you will learn Java Program to calculate Area and Perimeter of Right Triangle.


Related Tutorials…

Leave a Comment