 <p>Why not get the square root of the number? If its negative - java will throw an error and we will handle it.</p> <pre><code> try { d = Math.sqrt(THE_NUMBER); } catch ( ArithmeticException e ) { console.putln("Number is negative."); } </code></pre>