Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Heron\'s Formula for the area of a triangle with sides a, b and c is given by th

ID: 3582306 • Letter: H

Question

Heron's Formula for the area of a triangle with sides a, b and c is given by the formula: area = S(S-a) (S-b) (S-c) where s = (a+b+c)/2 Write a complete program which in main asks the user for the sides a, b and c of a triangle. Use variables of type double. Pass a, b and c to a function called area. In area calculate s defining s as a local variable. Then use s, a, b and c to calculate the area, passing the answer back to main. In main, print the sides and the area with 2 decimal places as follows: The area of a triangle with sides a, b and c is xx.xx Example do not use: The area of a triangle with sides 3, 4 and 5 is 6 00

Explanation / Answer

#include <iostream>

using namespace std;

/*method to calculate area of the triangle */
double area(double a,double b,double c){
double s=(a+b+c)/2; /*calculating s*/
/* calculating area */
double areaTriangle=s*(s-a)*(s-b)*(s-c);
return areaTriangle;/* returning area to main */
}
int main()
{double a,b,c;
/* Prompting user to enter 3 sides of the triangle */
cout << "Enter 1 side of the triangle : " << endl;
cin>>a;
cout << "Enter 2 side of the triangle : " << endl;
cin>>b;
cout << "Enter 3 side of the triangle : " << endl;
cin>>c;
/* calling area method and storing the returned result into variable areaTriangle */
double areaTriangle=area(a,b,c);
/* printing area */
cout<<"The area of a triangle with sides "<<a<<","<<b<<" and "<<c<<" is "<<areaTriangle<<endl;
return 0;
}

/*******OUTPUT**********
Enter 1 side of the triangle :
3   
Enter 2 side of the triangle :
4   
Enter 3 side of the triangle :
5   
The area of a triangle with sides 3,4 and 5 is 36   
*******OUTPUT************/

/* Note: Please ask in case of any doubt,thanks */