C++ Program To Find Area of Triangle Using Heron’s Formula
Heron’s formula is used to calculate area of triangle. To apply this formula, lengths of all three sides of triangle should be known.
Let’s suppose that a,b and c are three sides of a triangle, then Heron’s formula to find area of triangle would be:
In above formula, “A” is area of triangle while “s” is the half of triangle perimeter. To calculate “s”, we use
The formula is named after Heron of Alexandria, who was a Greek Engineer and Mathematician in 10 – 70 AD.
C++ Code To Find Area Of Triangle Using Heron’s Formula:
using namespace std;
cout<<"Area of triangle using Heron's formula"<<endl;
cout<<"Enter length of first side: ";
cout<<"Enter length of second side: ";
cout<<"Enter length of third side: ";
cout<<"Area of triangle: "<<area;
In this program, we included a header file named math.h. This file defines various mathematical functions like abs, pow, cos etc. We included this file in our program to use sqrt() function.
The program completes its processing in just two steps. First it takes lengths of three sides as input from user and calculates the half of triangle perimeter. The formula for calculation of half perimeter was:
In second step, it calculates the area of triangle. Here we used sqrt() function which calculates the square root. This function will not work if you don’t include math.h header file in your program.The formula for calculation of area was:
After that, it prints area of triangle on screen.
Recommended reading: Program To Find Area and Perimeter of Rectangle in C++
I want to mention here that if length of one side is bigger than length of other two sides combined then this shape is not considered triangle. In this case, the program will output area as zero.
If you liked this article, then please consider sharing it with your friends and leaving a comment below.
You can also Click Here To Download Exercise Files.