Given sum and product, find the two variables
Before we start guessing, lets solve the equations properly. We can substitute the equations so that we get product = x · (sum - x) = -x² + sum·x ⇔ 0 = x² - sum·x + product, with y = sum - x.

The equation can be solved by x = sum/2 ± √(sum²/4 - product) (see Reduced Quadratic Equation). Note that we get zero, one or two  distinct solutions. For example the special case of product = 0 leads to x = sum/2 ± sum/2 ⇒ {x = 0, x = sum}. It is up to you if you pick a specific solution or output both. We have no solutions in ℝ when the expression inside the square root is negative. However, there would still be complex solutions.

Restricting yourself to integers only isn't valid, and will miss some solutions. Consider product = 1, sum = 4. This is solved by x=3.73205, y=0.267949.

Note that as this equation can be solved arithmetically, there is no need for guessing via loops.



Because you resort to guessing, your code contains various bugs, e.g. it will fail to handle parameters with no solution like sum = 4, product = 5. Instead, you will go into an unterminated loop.

When unit-testing your code, you should be using a proper test harness instead of  some test cases in a main. You might like JUnit. If you want to thoroughly test a method, do not only test a few simple cases:


Explicitly test failure cases, e.g. parameters with no solution.
Test especially large inputs to see if you correctly guarded against overflows (you didn't).
Test random input (and record the seed!). It is easy to verify the correctness of the solution. This fuzzy testing may hit cases you didn't consider.
Cover inputs around zero especially well, e.g. vary each parameter from -2 to 2 by 0.5-increments (after verifying manually that each parameter combination actually has a solution). Although this wouldn't hit another bug in your code, the combination 0, 0 is suspiciously absent. The combination 1, 0 would have exposed the bug that you don't handle inputs without solutions.