I'm stuck on how I should make this program loop correctly to make it show all the triples of the pythagorean theorem that make perfect triangles. My instructor told me that my logic was correct but I know that my syntax is wrong. What should I change so that it shows all the triples up to 500 and display the count of them all?
#include <cstdlib>
#include <iostream>
usingnamespace std;
int pythagorean(int a, int b, int c)
int main(int argc, char *argv[])
{
cout << "Pythagorean triples: " << endl;
cout << pythagorean << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int pythagorean (int a, int b, int c){
int total;
for (int a=1;((a>=1)&&(a<500)); a++)
for (int b=1;((b>=1)&&(b<500)); b++)
for (int c=1;((c>=1)&&(c<500)); c++)
if ((a*a) + (b * b)) = c * c)
if ((a<b)&&(a<c)){
cout << a << " " << b << " " << c << endl;
total = total + 1;
}
}
When three integers a, b, and c satisfy the pythagorean equation, such as 3, 4, 5 do, the result is called a Pythagorean Triple. Create a program to list all of the Pythagorean Triples with integers of 500 or less. Also, give the total number of unique Pythagorean triples found by your program.
#include <iostream>
usingnamespace std;
int main()
{
for (int a = 1; a <= 500; ++a)
for (int b = a; b <= 500; ++b)
for (int c = b; c <= 500; ++c)
if (a * a + b * b == c * c)
cout << a << ' ' << b << ' ' << c << endl;
return 0;
}
I need to use a function in it and I fixed a few things which allowed it to run. However, when I run it now, the program repeats forever without stopping or using numbers that satisfy the pythagorean theorem. Here is what I have now:
#include <cstdlib>
#include <iostream>
usingnamespace std;
int pythagorean();
int main(int argc, char *argv[])
{
cout << "Pythagorean triples: " << endl;
int count = pythagorean();
cout << count << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int pythagorean ()
{
int total = 0;
for (int a=1; a<=500; a++)
{
for (int b=1; b<=500; b++) //if you change so that b is a + 1, you can get rid of an if statement. Do you see it?
{
for (int c=1; c<=500; c++) //this loop and the if following aren't completely necessary. You can use a and b to find c
{
if (a*a) + (b * b) == (c * c); //semicolon? Parentheses around the if?
{
if ((a<b)&&(a<c)) //a and b will always be less than c
{
cout << a << " " << b << " " << c << endl;
total = total + 1;
}
}
}
}
}
//return statement?
}
Wait, I tried to change some other stuff per Josue Molina's suggestion and now my program runs and hits a stop, but it doesn't have a count of how many there are and the first triple begins at 50, which I know is incorrect.
int pythagorean();
int main(int argc, char *argv[])
{
cout << "Pythagorean triples: " << endl;
int count = pythagorean();
cout << count << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int pythagorean (){
int total = 0;
for (int a=1; a<=500; a++)
{
for (int b=a; b<=500; b++) //you should start b at a + 1
{
for (int c=b; c<=500; c++) //yet again you should start at b + 1
{
if ((a*a) + (b * b) == (c * c))
{
cout << a << " " << b << " " << c << endl; //INDENTATIONS
total = total + 1; //INDENTATIONS
} //INDENTATIONS
}
}
}
return 0; //what is the return statement supposed to do?
}
You NEED indentations to make your code readable.
And your pythagorean triplets don't start at 50. The console can't display every line output.