Write a program that will read an arbitrary number of sets of triangle sides using only integer values.
You need to have functions for the following tasks:
Sample run:
Provide three side lengths – 0 0 0 to terminate.
3
5
4
3 5 4 Triangle possible: Scalene and Right
#include <iostream>
using namespace std;
int main() {
while (true) {
cout << "Provide three side lengths - 0 0 0 to terminate.\n";
int a, b, c;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) break;
cout << a << " " << b << " " << c << " ";
if (a > 0 && b > 0 && c > 0) {
if (a < b + c && b < a + c && c < a + b) {
cout << "Triangle possible: ";
if (a == b && b == c)
cout << "Equilateral";
else if (a == b || a == c || b == c)
cout << "Isosceles";
else
cout << "Scalene";
cout << " and ";
// set c as maximum side, to start checking Pythagorean theorem
if (a > c) swap(a, c);
if (b > c) swap(b, c);
int sum = a * a + b * b;
if (sum == c * c)
cout << "Right";
else if (sum > c * c)
cout << "Acute";
else
cout << "Obtuse";
}
else {
cout << "Triangle impossible: each side should be less than sum of other two";
}
}
else {
cout << "Triangle impossible: negative side(s)";
}
cout << endl;
}
return 0;
}
Comments
Leave a comment