Java Programs›Numbers›Find Square Root of a Number

Find Square Root of a Number in Java

beginner·  Numbers  ·  Math

Problem

The square root of x is the non-negative number r for which r × r = x; for example, the square root of 49 is 7.

Given a few numbers, print the square root of each, including a non-perfect square and a negative input.

Input
49, 50, 2.25, -9
Output
7.0, 7.0710678118654755, 1.5, NaN

Java Program

Java
public class SquareRoot { public static void main(String[] args) { double[] numbers = {49, 50, 2.25, -9}; for (double x : numbers) { double root = Math.sqrt(x); // NaN for negative input System.out.println("sqrt(" + x + ") = " + root); } System.out.printf("sqrt(50) to 3 places = %.3f%n", Math.sqrt(50)); } }

Output

sqrt(49.0) = 7.0 sqrt(50.0) = 7.0710678118654755 sqrt(2.25) = 1.5 sqrt(-9.0) = NaN sqrt(50) to 3 places = 7.071

Core Logic

Math.sqrt returns the correctly rounded square root as a double, so the interesting part is how it behaves at the edges.

How It Works
  1. 1The loop calls Math.sqrt(x) on each value in numbers.
  2. 2Perfect squares such as 49 come back exact (7.0); 50 gives a long decimal, the closest double to the true root.
  3. 3A negative input has no real square root, so Math.sqrt returns NaN (not a number) instead of throwing an exception.
  4. 4printf("%.3f") rounds the result for display without changing the stored value.
Math.sqrt(2.25) is 1.5, because 1.5 × 1.5 = 2.25.
💡

Key Point: Check for NaN with Double.isNaN(root) — comparing root == Double.NaN is always false, even when root is NaN.

Key Concepts

Math.sqrt()doubleNaNprintf formatting

Approach 2: Newton's Method

Java
public class SquareRootNewton { public static void main(String[] args) { double x = 50; double guess = x / 2; // any positive starting guess works int steps = 0; while (Math.abs(guess * guess - x) > 1e-9) { guess = (guess + x / guess) / 2; // average the guess with x / guess steps++; } System.out.printf("sqrt(%.0f) = %.6f after %d steps%n", x, guess, steps); } }

Output

sqrt(50) = 7.071068 after 6 steps

Core Logic

Newton's method improves a guess by averaging it with x divided by the guess; the error shrinks so fast that a handful of steps reach full precision.

How It Works
  1. 1guess starts at x / 2; any positive start converges.
  2. 2Each step sets guess = (guess + x / guess) / 2.
  3. 3If the guess is too big, x / guess is too small (and the other way round), so their average lands closer to the root.
  4. 4The loop stops once guess * guess is within 1e-9 of x.
For x = 50 the guesses run 25 → 13.5 → 8.6019 → 7.2073 → 7.0724 → 7.07107 → 7.0710678, so 6 steps reach the answer.
💡

Key Point: Each step roughly doubles the number of correct digits — the same idea hardware and math libraries use to compute roots quickly.

Complexity
Time Complexity: O(log d)Space Complexity: O(1)

Why: The number of correct digits roughly doubles per step, so reaching d digits of precision takes about log d iterations.

Key Concepts

iterationconvergenceMath.abs()

Interview Questions

How do you check whether a number is a perfect square?

long r = Math.round(Math.sqrt(n)); return r * r == n; — rounding guards against tiny floating-point errors.

What's the integer square root of a large number?

Use binary search on long values, or BigInteger.sqrt() (Java 9+), when exactness matters.

Learn the Concept

Related Programs