Hi, I'm not sure if I'm heading in the right direction, but here's what I need to do : 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.
This is just for my own practice, I'm trying to get better at problem solving and coding at the same time.
I'm pretty sure I have to use the pythogrean theorem a square + b square = c square
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int a,b,c;
cout << "Enter the length of three sides. " << endl;
cin >> a >> b >>c;
if(pow(c,2)-pow(b,2)-pow(a,2) == 0)
cout << "It is a right triangle " << endl;
elseif (pow(b,2)-pow(a,2)+pow(c,2) == 0 )
cout << "It's a right triangle" << endl;
elseif (pow(c,2) -pow(b,2)+pow(a,2)==0 )
cout << "It's a right triangle" << endl;
else
cout << "It is not a right triangle. " << endl;
return 0;
}
Found the problem, Apparently the first parameter has to be a double value, the variables you tried to get using cin were int. Change the variables to double and it should work out fine.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double a,b,c;
cout << "Enter the length of three sides. " << endl;
cin >> a >> b >>c;
if(pow(c,2)-pow(b,2)-pow(a,2) == 0)
cout << "It is a right triangle " << endl;
elseif (pow(b,2)-pow(a,2)+pow(c,2) == 0 )
cout << "It's a right triangle" << endl;
elseif (pow(c,2) -pow(b,2)+pow(a,2)==0 )
cout << "It's a right triangle" << endl;
else
cout << "It is not a right triangle. " << endl;
return 0;
}
Nvm, it turns out the numbers I was inputting could not be a right triangle, it does work because I tested it out, anyways thank you so much for the big help.