Project Euler problem#9 solution in C++

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Solution:

#include <iostream>
using namespace std;

int main()
{
int a;
int b;
int c;
for (a=1; a<=500; a++)
{
for (b=1; b<=500; b++)
{
c=1000-b-a;
if (a*a+b*b-c*c == 0 && a<b )
{
cout<<a<<” “<<b<<” “<<” “<<c<<” “<<a*b*c<<endl;
}
}
}
return 0;
}

Advertisements

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on April 22, 2013, in Programming and tagged , , , . Bookmark the permalink. 4 Comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: