Answer to Question #176963 in C++ for Luna

Question #176963

Write a program that will read an arbitrary number of sets of triangle sides using only integer values.  

  1. Prompt the user for sets of numbers and process them until the user submits the numbers 0 0 0, which will terminate the program.
  2. For each set of three numbers, the program should print the values read.
  3. For each set of three numbers, the program should decide if the numbers represent the sides of a valid triangle.
  4. If the numbers could not represent a valid triangle, display an appropriate error message.
  5. If the numbers are valid, the program should determine, and display, the:
  6. classify sides of the triangle – equilateral, isosceles, or scalene
  7. classify angles of the triangle – right, acute, or obtuse

You need to have functions for the following tasks:

  1. do the 3 side length form a triangle
  2. classify the triangle by side lengths
  3. classify the triangle as right or non-right triangle

Sample run:

Provide three side lengths – 0 0 0 to terminate.

3

5

4

3 5 4 Triangle possible: Scalene and Right


1
Expert's answer
2021-03-30T16:33:37-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS