Find square root and cube root in Java
sqrt(double s) method of Math class can be used to find the square root of the double or int value.
cbrt(double c) method of Math class can be used to find the cube root of the double or int value.
Both the methods are static, so it can be called directly using class name without creating object.
Example 1 (for sqrt)
class MathTest{
public static void main(String args[]){
double ds=5;
double sqr=Math.sqrt(ds);
System.out.println("Square root of value "+ds+" is "+sqr);
}
}
Output 1
Square root of value 5.0 is 2.23606797749979
Example 2 (for cbrt)
class MathTest{
public static void main(String args[]){
double dc=15;
double cbr=Math.cbrt(dc);
System.out.println("Cube root of value "+dc+" is "+cbr);
}
}
Output 2
Cube root of value 15.0 is 2.4662120743304703