The Math.pow() is an built-in method in Java Math
class and is used to calculate the power of a given number.
The power of a number refers to how many times to multiply the number with itself.
The signature of the pow()
method is as follows:
double
.double
.See the following code snippet for example:
double pow(double base, double exponent)
The pow()
method is a part of java.lang.Math
class, you need to import this class in your code to use the function.
Using the pow()
method let’s calculate the following:
import java.lang.Math;class CalculatePower {public static void main( String args[] ) {//Calculating 5^4 and casting the returned double result to intint ans1 = (int) Math.pow(5,4);System.out.println("5^4 is "+ans1);//Calculating 1.5^3 and storing the returned double result to ans2double ans2 = Math.pow(1.5,3);System.out.println("1.5^3 is "+ans2);//Calculating 100^5 and casting the returned double result to intdouble ans3 = Math.pow(50,5);System.out.println("50^5 is "+ans3);}}