Why does the answer print out twice?

Posted by rEgonicS on Stack Overflow See other posts from Stack Overflow or by rEgonicS
Published on 2010-05-10T03:25:20Z Indexed on 2010/05/10 3:28 UTC
Read the original article Hit count: 265

Filed under:

I made a program that returns the product a*b*c where a,b,c are pythagorean triples and add up to 1000. The program does output the correct answer but does it twice. I was curious as to why this is so.After playing around with it a bit I found out that it prints out when a = 200 b = 375 c = 425. And once again when a = 375 b = 200 c = 425. *How do you put your code into a "code" block? It does it automatically for part of the code but as you can see the top portion and bottom line is left out.

bool isPythagTriple(int a, int b, int c);

int main()
{

    for(int a = 1; a < 1000; a++)
    {
        for(int b = 1; b < 1000; b++)
        {
            for(int c = 1; c < 1000; c++)
            {
                if( ((a+b+c)==1000) && isPythagTriple(a,b,c) )
                {
                    cout << a*b*c << " ";
                    break;
                }
            }
        }
    }

    return 0;
}

bool isPythagTriple(int a, int b, int c)
{
    if( (a*a)+(b*b)-(c*c) == 0 )
        return true;
    else
        return false;
}

© Stack Overflow or respective owner

Related posts about c++