|
-
September 17th, 2009, 06:31 PM
#1
breaking out of a loop
hello. i am writing a program that prints all pythagorean triplets (ex. 3^2 + 4^2 = 5^2) until a + b + c = 1000. My code does that but keeps running after it finds that combination. I need it to print all the pythagorean triplets UP UNTIL it finds the combo (which happens to be 200, 375, 425) and then STOPS right away. As it stands right now it finds that combo and keeps going with the rest of the combinations that are still less than 1000. how can i stop the loops as soon as it finds the right combo?
here is my code:
Code:
//Written by: Jen G
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
int square;
int goal;
int sum;
for(a = 1; a < 1000; a++)
{
for(b = 1; b < 1000; b++)
{
if (a < b)
{
sum = (a * a) + (b * b);
for(c = 1; c < 1000; c++)
{
square = (c*c);
if (sum == square)
{
goal = a + b + c;
if (goal < 1000)
{
cout << "a: " << a << " b: " << b << " c: " << c << endl;
}
else if (goal == 1000)
{
cout << a << " + " << b << " + " << c << " = 1000" << endl;
cout << "abc = " << (a*b*c) << endl;
}
}
}
}
}
}
system("pause");
return 0;
}
Last edited by FeralDruidonWoW; September 17th, 2009 at 06:58 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|