Square root program in Java
In this post, you will learn how to write a program to find the square root of a number using the Java programming language.
In mathematics, the square root of a number is the number that we need to multiply by itself to get the original number. If a is the square root of b, it means that a×a=b. For example, the square root of 9, √9 = 3. Such a type of question is generally asked in competitive exams and interviews.
Square root in Java using sqrt() method
We can use the standard library function java.lang.Math.sqrt() to find the square root of a number. The sqrt() method returns the square root of a value of type double passed to it as an argument. We need to include the "java.lang.Math" library in the program to use this method.
Syntax of sqrt()
public static double sqrt(double x)
Here, x is the value whose square root is to be returned. If parameter x is positive double value, it will return the square root of x. If x is NaN or less than zero, this method will return NaN. If parameter x is positive infinity, this method will return a positive infinity. When x is positive or negative zero, this method will return the result as zero with the same sign.
import java.lang.Math;
public class SqRootProgram {
// driver code
public static void main(String args[])
{
double x = 20;
System.out.println(Math.sqrt(x));
x = 52;
System.out.println(Math.sqrt(x));
x = 32;
System.out.println(Math.sqrt(x));
x = 82;
System.out.println(Math.sqrt(x));
}
}
Output of the above code:
4.47213595499958
7.211102550927978
5.656854249492381
9.055385138137417
Square root in Java without using sqrt() method
In the given Java program, we find the square root of the specified numbers without using the sqrt() method.
// Java program to
// find floor of square root of given number
public class SquareRootProgram{
static int Sqrt(int a)
{
if (a == 0 || a == 1)
return a;
int i = 1, result = 1;
while (result <= a) {
i++;
result = i * i;
}
return i - 1;
}
// Driver program
public static void main(String[] args)
{
int x = 16;
System.out.print("Square root of "+ x +" is "+Sqrt(x));
int y = 25;
System.out.print("\nSquare root of "+ y +" is "+Sqrt(y));
}
}
Output of the above code:
Square root of 16 is 4
Square root of 25 is 5
Square root in Java using java.lang.Math.pow() method
We can use the java.lang.Math.pow() method of Java to find the square root. As the mathematical operation √number = number½ provides the square root of a number.
import java.util.Scanner;
public class SquareRootProgram {
public static void main(String[] args)
{
Double num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextDouble();
Double sqrt = Math.pow(num, 0.5);
System.out.println("Square root of " + num + " = " + sqrt);
}
}
Output of the above code:
Enter a number: 16
Square root of 16.0 = 4.0
Enter a number: 81
Square root of 81.0 = 9.0
Related Articles
Automorphic number in JavaPascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java