146k views
0 votes
Write a C++ program that accepts the lengths of three sides (a,b,c) of a triangle as input from the user. Validate the user input, so that sides a, b, c can only be POSITIVE. The program output should indicate whether or not the triangle is an Equilateral Triangle, a Right Triangle, Isosceles which is Not Equilateral, or some other type of triangle. Set up the program so the user can choose to repeat the program, if desired. Test your program for the 4 types of triangles, and submit the output with your C++ program. Use the Sample Run data.

1 Answer

5 votes

Final answer:

To write a C++ program that accepts the lengths of three sides of a triangle as input from the user, you can use the provided code which includes validation for positive side lengths. The program uses conditional statements to determine the type of triangle based on the lengths of the sides, and outputs the corresponding result.

Step-by-step explanation:

To write a C++ program that accepts the lengths of three sides of a triangle as input from the user, you can use the following code:

#include <iostream>
using namespace std;

int main() {
float a, b, c;
cout << "Enter the lengths of the three sides of the triangle: ";
cin >> a >> b >> c;

if (a > 0 && b > 0 && c > 0) {
if (a == b && b == c) {
cout << "The triangle is an Equilateral Triangle." << endl;
} else if (a == b || b == c || a == c) {
cout << "The triangle is an Isosceles which is Not Equilateral triangle." << endl;
} else if ((a * a == b * b + c * c) || (b * b == a * a + c * c) || (c * c == a * a + b * b)) {
cout << "The triangle is a Right triangle." << endl;
} else {
cout << "The triangle is some other type of triangle." << endl;
}
} else {
cout << "Please enter positive values for the lengths of the sides." << endl;
}

return 0;
}

This program prompts the user to enter the lengths of the three sides of a triangle and validates the input to ensure that the lengths are positive. It then uses conditional statements to determine the type of triangle based on the lengths of the sides. The program outputs whether the triangle is an Equilateral Triangle, a Right Triangle, Isosceles which is Not Equilateral, or some other type of triangle.

User IVR Avenger
by
7.5k points