Calculating the square root of a number is a common requirement in programming, especially in mathematical and scientific applications. This tutorial from The Coding College will show you how to compute the square root in Java using various methods.
What is a Square Root?

Java provides built-in methods and manual approaches to find the square root.
Methods to Calculate Square Root
1. Using the Math.sqrt()
Method
The simplest way to calculate the square root in Java is by using the Math.sqrt()
method.
import java.util.Scanner;
public class SquareRootExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input from user
System.out.print("Enter a number: ");
double number = scanner.nextDouble();
if (number < 0) {
System.out.println("Square root of a negative number is not real.");
} else {
// Calculate square root
double squareRoot = Math.sqrt(number);
System.out.println("The square root of " + number + " is: " + squareRoot);
}
}
}
Output Example:
Enter a number: 16
The square root of 16.0 is: 4.0
Enter a number: -4
Square root of a negative number is not real.
2. Using Manual Approximation
If you want to compute the square root without using Math.sqrt()
, you can use a manual approximation method like Newton’s method.
public class SquareRootApproximation {
public static double squareRoot(double number) {
if (number < 0) {
throw new IllegalArgumentException("Number must be non-negative");
}
double guess = number / 2;
double epsilon = 1e-7; // Precision level
while (Math.abs(guess * guess - number) > epsilon) {
guess = (guess + number / guess) / 2;
}
return guess;
}
public static void main(String[] args) {
double number = 49;
double result = squareRoot(number);
System.out.println("The square root of " + number + " is approximately: " + result);
}
}
3. Using Iterative Methods
You can also implement a loop-based approximation.
public class IterativeSquareRoot {
public static double findSquareRoot(double number) {
if (number < 0) {
throw new IllegalArgumentException("Number must be non-negative");
}
double low = 0;
double high = number;
double mid = 0;
double epsilon = 1e-7;
while (high - low > epsilon) {
mid = (low + high) / 2;
if (mid * mid > number) {
high = mid;
} else {
low = mid;
}
}
return mid;
}
public static void main(String[] args) {
double number = 10;
double result = findSquareRoot(number);
System.out.println("The square root of " + number + " is approximately: " + result);
}
}
Real-Life Applications
- Engineering: Square root calculations are essential for physics and electrical engineering problems.
- Graphics: Used in computer graphics for scaling and geometric calculations.
- Finance: Square roots are often used in risk analysis and statistical calculations.
Best Practices
- Handle Negative Inputs: The square root of negative numbers is not real unless you consider complex numbers.
- Precision Control: Ensure high precision for applications requiring accurate results.
- Performance: Use built-in methods for efficiency unless custom approximations are needed.
Conclusion
Calculating the square root of a number in Java is straightforward with the Math.sqrt()
method, but alternative methods like Newton’s or iterative approximation can also be useful. Understanding these approaches helps you handle diverse scenarios effectively.
For more Java tutorials, visit The Coding College and enhance your coding skills!