Java Program to Find the Area of a Triangle
A triangle is a polygon. It has three edges and three vertices and each vertex from an angle. It is a closed 2-dimensional shape. In this article, we will learn how to find the area of the triangle.
There can be two possibilities while calculating the area of the triangle as per cases
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- Using the height and base of the triangle
- Using the 3 sides of the triangle
Case 1: When the height and base of the triangle are given, then the area of the triangle is half the product of its base and height.
Area of triangle: Area = (height×base)/2
Example 1: Evaluation of area using base and height
Area of the triangle: 25.0
Case 2: When the three sides of the triangle are given
Now suppose if only sides are known to us then the above formula can not be applied. The area will be calculated using the dimensions of a triangle. This formula is popularly known as Heron’s formula.
- The Semiperimeter of the triangle is calculated.
- Product of semi meter with 3 values where these rest of values are the difference of sides from above semi perimeter calculated.
- Square rooting the above value obtained from the computations gives the area of a triangle.
Area of the triangle: 14.6969385