Java Program to Find Square Root of a Number
You can find the square root of a number in Java using
Math.sqrt(number), for example: double root = Math.sqrt(25); which returns 5.0.Examples
Input25
Output5.0
Input0
Output0.0
Input2
Output1.4142135623730951
How to Think About It
To find the square root, think about what number multiplied by itself gives the original number. We use Java's built-in
Math.sqrt() method which calculates this value directly and accurately.Algorithm
1
Get the input number from the user.2
Use the built-in function to calculate the square root.3
Display the result to the user.Code
java
import java.util.Scanner; public class SquareRoot { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double number = scanner.nextDouble(); double root = Math.sqrt(number); System.out.println("Square root of " + number + " is " + root); scanner.close(); } }
Output
Enter a number: 25
Square root of 25.0 is 5.0
Dry Run
Let's trace the input 25 through the code
1
Input number
User enters 25, stored in variable number = 25.0
2
Calculate square root
Call Math.sqrt(25.0), result is 5.0 stored in root
3
Print result
Prints: Square root of 25.0 is 5.0
| Step | Variable | Value |
|---|---|---|
| 1 | number | 25.0 |
| 2 | root | 5.0 |
| 3 | output | Square root of 25.0 is 5.0 |
Why This Works
Step 1: Input reading
We read the number from the user using Scanner to get the value to find the square root of.
Step 2: Square root calculation
We use Math.sqrt() which is a built-in Java method that returns the square root of the given number.
Step 3: Output display
We print the result so the user can see the square root value.
Alternative Approaches
Using exponentiation operator
java
import java.util.Scanner; public class SquareRoot { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double number = scanner.nextDouble(); double root = Math.pow(number, 0.5); System.out.println("Square root of " + number + " is " + root); scanner.close(); } }
Uses <code>Math.pow()</code> with 0.5 exponent to find square root; slightly less direct but works the same.
Using Newton's method (manual calculation)
java
import java.util.Scanner; public class SquareRoot { public static double sqrtNewton(double n) { double x = n; double y = 1; double e = 0.000001; // error tolerance while (x - y > e) { x = (x + y) / 2; y = n / x; } return x; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double number = scanner.nextDouble(); double root = sqrtNewton(number); System.out.println("Square root of " + number + " is " + root); scanner.close(); } }
Manually calculates square root using Newton's iterative method; useful for learning but more complex.
Complexity: O(1) time, O(1) space
Time Complexity
The calculation uses a built-in method that runs in constant time, so it is O(1).
Space Complexity
Only a few variables are used, so space complexity is O(1).
Which Approach is Fastest?
Using Math.sqrt() is fastest and simplest compared to manual methods like Newton's method.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Math.sqrt() | O(1) | O(1) | Quick and accurate calculation |
| Math.pow(number, 0.5) | O(1) | O(1) | Alternative using exponentiation |
| Newton's method | O(k) where k is iterations | O(1) | Learning manual calculation, customizable precision |
Use
Math.sqrt() for a quick and accurate square root in Java.Forgetting to handle negative inputs, which cause
Math.sqrt() to return NaN.