Problem with solving pythagorean triple problem

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;  
}
Last edited on
In C++, a^2 isn't a2, it's a XOR 2.

Use a*a to get a2. For larger powers, there's the pow() function in <cmath>
In C++, a^2 isn't a2, it's a XOR 2

*face palm* https://tenor.com/view/hopeless-disappointed-ryan-reynolds-facepalm-embarrassed-gif-5436796

Been into C++ for a couple of years now, feel so stupid to only find that out now.. I was actually trying earlier today to raise a number to some power with the operator '^' and ended up dumping it because It was clearly not doing what I expected..
Topic archived. No new replies allowed.