Hi, I'm trying to write a code that produce all the first 100 pythagorean triples but by the time I'm only capable of obtain the first one (3, 4, 5) and I don't understand why. Please, help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
usingnamespace std;
int main ()
{
for (int x = 0, y = 1, z = 2; x <= 100; x++, y++, z++)
{
if (((x*x) + (y*y)) == z*z)
{
cout << x << y << z << endl;
}
}
system("pause");
return 0;
}
You should use a double for loop.
Below is a corrected code.
#include<iostream>
using namespace std;
int main ()
{
for (int x = 0; x <= 100; x++)
{
for (int y = 1, z = 2; y <= 100; y++, z++)
{
if (((x*x) + (y*y)) == z*z)
{
cout << x << y << z << endl;
}
}
}
system("pause");
return 0;
}
int main ()
{
for (int x = 0; x <= 100; x++)
{
for (int y = 1; y <= 100; y++)
{
for (int z = 2; z <= 100; z++)
{
if (((x*x) + (y*y)) == z*z)
{
cout << x << ", " << y << ", " << z << endl;
}
}