So the problem is a user inputs a number and you must find if any of the possible sums of that number are pythagorean triples, for example 12=3+4+5 and also 3^2 +4^2 = 5^2 so if the user inputted 12 then the program would output 3, 4, 5 or any other possible combinations if there is more than one.
So the problem I am having is that the code spits out the correct values it does 3, 4, 5 and then 4, 3, 5 and then it puts out 2, 5, 5 !!!??
This makes no sense to me, in the if statement I specify that if( ((a^2) + (b^2)) == (c^2)) yet for some reason 2, 5, 5 is evaluating to true and executing the code inside the brackets of the if statement.
Any help would be appreciated, I am a total beginner, but I can't find the error. Thank you.
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 29 30 31 32 33 34 35
|
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a =1;
int b = 1;
int c = n-2;
int x = 3;
while(b < c)
{
while(a < c)
{
if( ((a^2) + (b^2)) == (c^2) )
{
cout<< "these are your pythagorean triple numbers: " << a << " "<< b << " "<< c << endl;
break;
}
else
{
++a;
--c;
}
}
c = n-x;
++x;
a=1;
++b;
}
return 0;
}
|