Return the product of the pythagorean triplet if a+b+c == 1000 I have found the answer to the problem I am trying to solve, but I am curious if there can be any improvements made to my code.

#include &lt;iostream&gt;
#include &lt;limits&gt;
#include &lt;cmath&gt;
using namespace std;

unsigned long pythagoreanTriplet();
int main() {
cout &lt;&lt; pythagoreanTriplet() &lt;&lt; endl;
}

unsigned long pythagoreanTriplet()
{
int a,b;
   unsigned long c;
for(a = 1; a &lt; 1000;a++)
{
    for(b = a+1; b &lt;= 1000;b++)
    {
        c = sqrt(pow(a,2)+pow(b,2));

        if(pow(a,2)+pow(b,2) == pow(c,2))
        {
            if(a+b+c == 1000)
            {
                unsigned long product = a*b*c;
                return product;
            }
        }
    }
 }
}
Don't use std::pow(n, 2) to square an integer - not only is it slower than n*n, it's much less accurate (so you'd want to add 0.5 before assigning to c, to round to nearest).

Instead of computing c as the hypotenuse of triangle (a,b,c), it's simpler to take c as 1000-a-b, then check if that makes a right-triangle:
        c = 1000 - a - b;
        if (a*a + b*b == c*c) {
            return a*b*c;
        }

Look, no &lt;cmath&gt;!

We can reduce the limits of our search, by noting that a can't be as much as 500 (because a &lt; b and a+b &lt; 1000):
for (a = 1;  a &lt; 500;  ++a) {
    for (b = a+1;  b &lt; 1000-a;  ++b) {
        c = 1000 - a - b;


Keep your types consistent.  You seem to be aware that int is only guaranteed to be able to hold values up to 32,767, so that's obviously the wrong choice for c and for the result.  But it's also the wrong choice for a and b, because expressions such as a*b can overflow (500*500 = 250,000), so that computing the return value could be Undefined Behaviour.  The best approach is to use one of the fixed-size types defined in &lt;cstdint&gt; for all three quantities - I recommend std::uint_fast32_t for this.

Make sure you return a value even if no match was found.  At present, we just run off the end of the function (and get a compiler warning, so we have no excuse for missing that).  We can safely return zero to indicate &quot;no match&quot;, as that could never be the result of a successful search.

Modified code
#include &lt;iostream&gt;
#include &lt;cstdint&gt;

std::uint_fast32_t pythagoreanTriplet()
{
    for (std::uint_fast32_t a = 1;  a &lt; 500;  ++a) {
        for (auto b = a+1;  b &lt; 1000-a;  ++b) {
            auto c = 1000 - a - b;
            if (a*a + b*b == c*c) {
                return a*b*c;
            }
        }
    }
    return 0;                   // not found
}

int main()
{
    std::cout &lt;&lt; pythagoreanTriplet() &lt;&lt; std::endl;
}


Exercise
Instead of hard-coding the target sum 1000, pass it as a parameter to your function.