Need help, please!

closed account (yR9wb7Xj)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace 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;
	 else if (pow(b,2)-pow(a,2)+pow(c,2) == 0 )
		 cout << "It's a right triangle" << endl;
	 else if (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;
}
Last edited on
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.
closed account (yR9wb7Xj)
I did that but it's still not working, should I use float instead of double?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>
using namespace 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;

	 else if (pow(b,2)-pow(a,2)+pow(c,2) == 0 )
		 cout << "It's a right triangle" << endl;
	 else if (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;
}
When I compiled it, it worked. What kind of error did it say?
Also try putting in something like system("Pause"); so that you can see the message for more than a second.
closed account (yR9wb7Xj)
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.
Last edited on
Topic archived. No new replies allowed.