Math pow() method is used to calculate the value of a power b (first argument raised to the power of the second argument) with two double parameters
Java Math.pow Method
Method Syntax:
a^b = Math.pow(double a, double b)
Example 1: Calculate 2^3 with Math.pow() method.
System.out.println(Math.pow(2, 3)); //8.0
Example 2: Calculate square root of 4 with Math.pow() method.
double num = 4; double sqr = Math.pow(4, 0.5); System.out.println(sqr); //2.0
Example 3: Calculate result of given operation (i and r should be user input)
Scanner input = new Scanner(System.in); System.out.print("i= > "); int i = input.nextInt(); System.out.print("r= > "); int r = input.nextInt(); double result = r*(1/Math.pow((1+i)/2.0 , 2) / (Math.pow(i, 1/3.0))); System.out.println("result: "+result);
Console output:
i= > 6 r= > 20 result: 0.8984836051413949