、Giventhatthesidesofatriangleare5,6and8cm,findthelengthandareaofthetriangle. Attentionused“cin”and“cout”towritingprogram.
时间: 2024-02-17 09:04:08 浏览: 14
Sure, here's a C++ program that uses "cin" and "cout" to solve the problem:
```C++
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// Input the sides of the triangle
double a, b, c;
cout << "Enter the sides of the triangle (in cm): ";
cin >> a >> b >> c;
// Check if the sides form a valid triangle
if (a + b <= c || b + c <= a || a + c <= b)
{
cout << "Invalid triangle" << endl;
return 0;
}
// Calculate the length of the altitude from side a
double s = (a + b + c) / 2;
double area = sqrt(s * (s - a) * (s - b) * (s - c));
double h = 2 * area / a;
// Output the length and area of the triangle
cout << "Length of altitude from side a: " << h << " cm" << endl;
cout << "Area of the triangle: " << area << " cm^2" << endl;
return 0;
}
```
When executed, the program will prompt the user to input the sides of the triangle, and then calculate the length of the altitude from side a and the area of the triangle. Note that we first check if the sides form a valid triangle using the triangle inequality theorem.