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:arae of triangle in c++

In above formula, “A” is area of triangle while “s” is the half of triangle perimeter. To calculate “s”, we usearea of triangle in c++

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;
int main()
	float a,b,c,s,area;
	cout<<"Enter length of first side: ";
	cout<<"Enter length of second side: ";
	cout<<"Enter length of third side: ";
	cout<<"Area of triangle: "<<area;
	return 0;

Code Explanation:

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.

Output:area of triangle in c++

 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.