iventhatthesidesofatriangleare5,6and8cm,findthelengthandareaofthetriangle. Attentionused“cin”and“cout”towritingprogram.
时间: 2024-02-20 16:56:14 浏览: 10
Sure! Here's a C++ program that uses the Heron's formula to find the area of the triangle and checks if the sides form a valid triangle using the triangle inequality theorem, and reads input from the user using `cin` and outputs the results using `cout`:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cout << "Enter the lengths of the three sides of the triangle: ";
cin >> a >> b >> c;
double s = (a + b + c) / 2.0;
double area = sqrt(s * (s - a) * (s - b) * (s - c));
if (a + b > c && a + c > b && b + c > a) {
cout << "The sides form a valid triangle." << endl;
cout << "The length of the remaining side is between " << abs(b - c) << " and " << b + c << " cm." << endl;
cout << "The area of the triangle is " << area << " cm^2." << endl;
} else {
cout << "The sides do not form a valid triangle." << endl;
}
return 0;
}
```
When you run this program, it will prompt you to enter the lengths of the three sides of the triangle. You can enter 5, 6, and 8, and the program will output:
```
The sides form a valid triangle.
The length of the remaining side is between 2 and 14 cm.
The area of the triangle is 14.9812 cm^2.
```
Note that the program reads input from the user using the `cin` function, and outputs the results using the `cout` function.