C++ Program to Fin Area of the Triangle














































C++ Program to Fin Area of the Triangle



Area of a Triangle can be evaluated using following formula.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where a, b and c are lengths of sides of triangle and s = (a+b+c)/2
Example:
Input : a = 3, b = 4, c = 5
Output : Area of a triangle is 6.0

#include<iostream>

#include<math.h>
using namespace std;

// Function to find area of triangle
float findArea(float a, float b, float c)
{
// Length of sides must be positive
// Sum of any two sides must be smaller than third side.
if (a < 0 || b < 0 || c <0 || (a+b <= c) || a+c <=b || b+c <=a)
{
cout<<"Not a valid triangle";
exit(0);
}
float s = (a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}

int main()
{
float a = 3.0; // Length of side a
float b = 4.0; // Length of side b
float c = 5.0; // Length of side c
cout<<"Area of triangle : "<<findArea(a,b,c);
return 0;
}