18.7k views
5 votes
In a right triangle, the square of the length of one side is equal to the sum of the squares of the lengths of the other two sides. Write a program that prompts the user to enter the lengths of three sides of a triangle and then outputs a message indicating whether the triangle is a right triangle.

User Fiw
by
6.0k points

1 Answer

0 votes

Answer:

The c++ program to determine if the entered sides form a right-angle triangle or not is given below.

#include <iostream>

using namespace std;

int main() {

double h, b, hyp, sum, s;

cout << "Enter the longest side " << endl;

cin >> hyp;

cout << "Enter the height " << endl;

cin >> h;

cout << "Enter the breadth " << endl;

cin >> b;

sum = (h*h) + (b*b);

s = (hyp*hyp);

if (sum == s)

cout << "The triangle with the given sides forms a right triangle." << endl;

else

cout << "The triangle with the given sides does not forms a right triangle." << endl;

return 0;

}

OUTPUT

Enter the longest side

5

Enter the height

4

Enter the breadth

3

The triangle with the given sides forms a right triangle.

Step-by-step explanation:

The variables are declared as double to accommodate both integers and decimal values.

The program works as explained below.

1. This program is designed to take the values of all the three sides from the user.

2. User input is not validated since it is not mentioned in the question.

3. The square of the longest side is stored in a separate variable, s.

4. Similarly, sum of the squares of the other two sides is stored in a separate variable, sum.

5. If both the values, square of the longest side and the sum of the squares of the other two sides are equal, a message is displayed that the three sides form a right triangle.

6. If both the values of the squares are different, a message is displayed that the three sides do not form a right triangle.

7. The program terminates with a return statement.

8. The standard input and output is done using cin and cout keywords.

9. The endl keyword is used to insert new line. This is done to improve readability of the program.

10. The language used is c++ due to its simplicity. The logic remains the same.

User Matias Diez
by
5.2k points