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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.*; public class AreaOfTriangle { public static void main(String[] args) { double a, b, c, S, area; Scanner in = new Scanner(System.in); // Asking the user to enter three sides of the triangle System.out.println(" Enter the three sides of triangle :"); a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); S = (a + b + c) / 2; //Finding semi perimeter of triangle area = Math.sqrt(S * (S - a) * (S - b) * (S - c)); //Calculating area of triangle using Heron'sformula System.out.println(" Area of Triangle : " + area +" square units"); } } |
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
1 2 3 |
Enter the three sides of triangle : 2 3 4 Area of Triangle : 2.9047375096555625 square units |
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…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area and Perimeter of Rectangle
- Java Program to calculate Area and Perimeter of Square
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Area and Volume of Cuboid
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Volume of Cone
- Java Program to calculate Surface Area and Volume of Sphere