Hello all. Just starting out learning to code sorry if this is really basic. I need to write a program inputs three sides to a triangle and determines if said triangle is a right triangle. The order of the numbers has to be random so you can't ask the user to input the largest side, etc.
For the life of me I can not figure out this if else if thing. I've gotten to the point where I would input 3, 4, 5 and random orders and the program works as it should but when other numbers were entered the program would end without a conclusion statement so I gave up.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int side1, side2, side3;
// Formula: a^2 + b^2 = c^2 (3,4,5)
cout << "Enter the length of the first side: ";
cin >> side1;
cout << "Enter the length of the second side: ";
cin >> side2;
cout << "Enter the length of the third side: ";
cin >> side3;
if (side1 > side2 && side1 > side3)
{
if ((side1 ^ 2) == (side2 ^ 2 + side3 ^ 2))
cout << "This is a right triangle." << endl;
else
cout << "This is NOT a right triangle." << endl;
}
return 0;
}
This new program however seems to be working in the exact opposite way of how it should. Now I don't even know how I should start. Someone please tell me how I should be approaching this!
You have the right idea, but you're covering only one case (where side 1 is the longest). You need to do the same thing for side 2 and side 3. Keep in mind that if side 1 is not > than side 2 and 3 and side 2 is not greater than side 1 and 3, then side 3 must be the hypotenuse.
Line 27: ^ is not an exponentiation operator. ^ is the exclusive or (XOR) bitwise operator.