In this tutorial, we are going to learn writing java program to calculate the square root of a given number.
Our Program will take a number from the users as an input, and will return the square root of that number as output.
What is Square root of Number?
A square root of a number is a value that, when multiplied by itself, produces the original number. In Java, we can calculate the square root of a number using the built-in Math.sqrt() method.
For example, the square root of 25 is 5 because 5 × 5 = 25.
In this program, we will take a number as input from the user and calculate its square root using Java.
For example
2 is a square root of 4. -2 is also a square root of 4.
because 2*2 = 4 and -2 * -2 = 4
Java Program to calculate square root of number
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
double num = sc.nextDouble();
if (num < 0) {
System.out.println("Square root of a negative number is not a real number.");
} else {
double result = Math.sqrt(num);
System.out.println("Square root of " + num + " is " + result);
}
sc.close();
}
}
Output
Please give a number to calculate Square root
25
Square Root of 25.0 is
5.00
Explanation
For input 25, the output generated by our program is math.sqrt(25.0) which is equal to 5.00.
How Does the Java Program Work?
Let’s understand the program step by step.
- Import Scanner class: The
Scannerclass is imported to accept input from the user. - Create Scanner object: We create a Scanner object to read the number entered through the keyboard.
- Read the input: The
nextDouble()method accepts an integer or decimal number and stores it in thenumvariable. - Check the number: The
ifcondition checks whether the entered number is negative. Negative numbers do not have real-valued square roots. - Calculate square root: The
Math.sqrt(num)method calculates the square root of the given number. - Display the result: The
println()statement displays the calculated square root.
Math.sqrt() Method in Java
The Math.sqrt() method is a built-in mathematical function in Java that returns the square root of a given number.
Syntax:
Math.sqrt(number);
Parameter:
– number: The numeric value whose square root needs to be calculated. It is passed as a double.
Return type:
– double: Returns the square root of the specified number.
Examples using Math.sqrt():
| Expression | Result |
|---|---|
Math.sqrt(25) | 5.0 |
Math.sqrt(36) | 6.0 |
Math.sqrt(49) | 7.0 |
Math.sqrt(2) | 1.4142135623730951 |
Math.sqrt(0) | 0.0 |
Math.sqrt(-9) | NaN |
Important: The Math.sqrt() method returns NaN (Not a Number) for negative values. Therefore, it is good programming practice to validate the input before calculating the square root.
Java Program to Find Square Root Without Using Math.sqrt()
We can also calculate the square root of a number without using the built-in Math.sqrt() method.
One approach is to use the Newton-Raphson method, which repeatedly improves an estimated square root until the required precision is achieved.
Java Program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
double num = sc.nextDouble();
if (num < 0) {
System.out.println("Square root is not a real number.");
} else if (num == 0) {
System.out.println("Square root is 0.0");
} else {
double guess = num;
double epsilon = 0.000001;
while (Math.abs(guess - num / guess) > epsilon) {
guess = (guess + num / guess) / 2;
}
System.out.println("Square root of " + num + " is " + guess);
}
sc.close();
}
}
Output:
Enter a number: 25
Square root of 25.0 is 5.0
Explanation of the program to Find Square Root Without Using Math.sqrt()
- Initially, we assume the given number itself as the square root estimate.
- We calculate a better estimate using the Newton-Raphson formula.
- The formula is:
Where:
- x = Current estimate.
- n = Given number.
- xnew = Improved estimate.
- The loop continues until the difference between successive estimates becomes sufficiently small.
- Finally, the calculated square root is displayed.