In: Computer Science
Q-2) In a right triangle, the square of the length of one side is equal to the sum of 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 or not.
Since you have not mentioned the programming language, I have answered in C++. In case, you need any other programming language, then let me know in the comment section.
Please look at my code and in case of indentation issues check the
screenshots.
-------------main.cpp---------------
#include <iostream>
using namespace std;
int main()
{
float side1, side2, side3;
cout << "Enter the length of side1:
"; //read 3 sides
cin >> side1;
cout << "Enter the length of side2: ";
cin >> side2;
cout << "Enter the length of side3: ";
cin >> side3;
if(side1*side1 == side2*side2 +
side3*side3) //check if sum squares
of side2, side3 is equal to side1 square
cout << "The triangle is a
right angled triangle" << endl;
else if(side2*side2 == side1*side1 +
side3*side3) //check if sum squares of side1, side3 is
equal to side2 square
cout << "The triangle is a
right angled triangle" << endl;
else if(side3*side3 == side1*side1 +
side2*side2) //check if sum squares of side1, side2 is
equal to side3 square
cout << "The triangle is a
right angled triangle" << endl;
else
cout << "The triangle is a
NOT a right angled triangle" << endl;
return 0;
}
--------------Screenshots--------------
--------------------Output-----------------------
----------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou