๐งฎ Java Program to Calculate Square Root โ With Examples and Best Practices
๐งฒ Introduction
Calculating the square root is a common requirement in mathematical and scientific applications. Java provides a simple yet powerful way to calculate square roots using built-in methods from the Math
class.
By the end of this guide, you’ll be able to:
- โ
Calculate square roots using
Math.sqrt()
- โ Handle invalid input like negative numbers
- โ Build user-interactive and reusable Java programs
๐ What is Square Root?
The square root of a number x
is a value y
such that:
y ร y = x
In Java, this is done using the built-in Math.sqrt()
method.
๐ป Java Program โ Hardcoded Input
public class SquareRootExample {
public static void main(String[] args) {
double number = 25;
double result = Math.sqrt(number);
System.out.println("Square root of " + number + " is: " + result);
}
}
โ Explanation:
Math.sqrt(25)
returns5.0
.double
is used for decimal precision.
๐ฅ Java Program โ User Input with Scanner
import java.util.Scanner;
public class SquareRootUserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
double number = scanner.nextDouble();
if (number < 0) {
System.out.println("Error: Cannot calculate square root of a negative number.");
} else {
double result = Math.sqrt(number);
System.out.println("Square root of " + number + " is: " + result);
}
scanner.close();
}
}
โ Explanation:
- Takes user input.
- Checks for negative values before computing square root.
๐ Java Method โ Reusable Function
public class SquareRootUtil {
public static double computeSquareRoot(double number) {
if (number < 0) {
throw new IllegalArgumentException("Negative input is not allowed");
}
return Math.sqrt(number);
}
public static void main(String[] args) {
double value = 16;
System.out.println("Square root: " + computeSquareRoot(value));
}
}
โ Best for modular code and reusability in larger projects.
โ ๏ธ Edge Case Warning
double result = Math.sqrt(-9); // NaN
System.out.println(result); // Output: NaN
โ ๏ธ Math.sqrt()
returns NaN
(Not-a-Number) for negative inputs, not an exception. Handle it manually for cleaner logic.
๐ Sample Output
Enter a number: 36
Square root of 36.0 is: 6.0
Enter a number: -5
Error: Cannot calculate square root of a negative number.
๐ Summary
In this tutorial, youโve learned:
- โ
How to calculate the square root in Java using
Math.sqrt()
- โ Handle user input and negative numbers
- โ Create reusable functions for cleaner code
This logic is widely used in geometry, physics, finance, and UI scaling.
โ FAQ โ Square Root in Java
โCan I calculate square root of negative numbers?
No, not directly. Java returns NaN
for negative inputs. Use Math.sqrt()
with a manual check.
โWhich data type is best for square root?
Use double
for precision. Avoid int
, as square roots often result in fractions.
โIs there a way to round the square root result?
Yes:
double result = Math.sqrt(50);
System.out.println(Math.round(result)); // Rounds to nearest whole number
Share Now :