The area of a triangle is the region enclosed within the sides. The area varies from one triangle to another depending on the length of the sides and the internal angles.
Heron's formula is used to calculate the triangle's area when the length of the 3 sides is known. To use this formula, we need to know the perimeter, which is the distance covered around the triangle and is calculated by adding the length of all three sides.
The perimeter of a triangle is defined as the total length of its boundary, which means the sum of all three sides. To calculate the perimeter, we simply add the lengths of the three sides given.
Perimeter = a + b + c, where a, b, c are sides
Ask the user to enter the length of all three sides. With these side lengths, apply the formula as given above and initialize its value to a variable say the area, and print its value as output.
Create a class with a named triangle. Declare float type variables a, b, c, s, and aa privately to store the side lengths are semi-perimeter and area. Declare three functions for getting the input data, calculating the area, and calculating the perimeter.
within the main function create an object t for the class triangle. triangle t;
Declare two float type variables ar and pr to get the area and perimeter of the triangle. Call the function to get the input. Now call the function area with the object t and store the value in the variable ar and call the function perimeter with the object t and store the value into the variable pr. Display the result.
Step 1: Call the header file iostream.
Step 2: Use the namespace std.
Step 3:Create a class triangle;
Step 4: Declare float type variable a, b, c, s, aa;
Step 5: Define the function for getting the input.
Ask the user to enter the length of the three sides
Read the value to the variable a, b, c;
Step 6: Define the function for calculating the area of the square.
s = (a+b+c)/2;
aa = sqrt(s*(s-a)*(s-b)*(s-c));
Step 7: Define the function for calculating the perimeter of the square.
a + b + c;
Step 8: Open the integer type main function; int main().
Step 9: Create an object t for the class triangle;
Step 10: Declare two float type variables ar and pr;
Step 11: get the function to read the input
t.step 5;
Step 12: Get the area to the variable ar
ar = t.step 6;
Step 13: get the perimeter to the variable pr
Pr = t.step 7;
Step 14: Display the value of ar and pr;
Step 15: Exit
#include<iostream>
#include<cmath>
using namespace std;
class triangle
{
private:
float a, b, c, s, aa;
public:
void getdata();
float area();
float perimeter();
};
void triangle::getdata()
{
cout<<"Enter the Length of all Three Sides: ";
cin>>a>>b>>c;
}
float triangle::area()
{
s = (a+b+c)/2;
aa = sqrt(s*(s-a)*(s-b)*(s-c));
return aa;
}
float triangle::perimeter()
{
return (a+b+c);
}
int main()
{
triangle t;
float ar, pr;
t.getdata();
ar = t.area();
cout<<"\n Area = "<<ar;
pr = t.perimeter();
cout<<"\nPerimeter = "<<pr;
cout<<endl;
return 0;
}
Enter the Length of all Three Sides: 6 5 9 Area = 14.1421 Perimeter = 20