C++ Program to Find Area of a Triangle

- March 15, 2018
To calculate the area of a triangle we need to have the lengths of all three sides, using "Heron's Formula" that has been known for nearly 2000 years.
finding area of triangle in cpp

The following simple program inputs three sides of a triangle from the user and finds out triangle area by Heron's Formula:
 Heron's method A = s (s-a) (s-b) (s-c) under root

Where:
s = (a + b + c)/2

Program Source Code

#include<iostream>
#include<math.h>
using namespace std;
int main()
 {
   float s, a, b, c, area;
   std :: cout<<"Enter first side of triangle:\n";
   std :: cin>>a;
   std :: cout<<"Enter second side of triangle:\n";
   std :: cin>>b;
   std :: cout<<"Enter third side of triangle:\n";
   std :: cin>>c;
          s = (a+b+c) / 2.0;
   area = sqrt (s*(s-a)*(s-b)*(s-c));
   std::  cout<<"The sides of triangle = "<<a<<"," <<b<< ","<<c;
   std :: cout<<"\n Area of triangle = "<<area;
   return 0;
 }

Output of Program

Enter first side of triangle: 30
Enter second side of triangle: 40
Enter third side of triangle: 60

The sides of triangle = 30, 40, 60
Area of triangle = 533.268

 

Start typing and press Enter to search