Using the pow() method
To use the pow()
method, call the Math.pow()
function and pass in the base and power values as arguments. The general signature of the method is shown below.
public static double pow(double base, double power)
Add the following code to the main
function after the import
statement.
double d1 = Math.pow(3, 5);
double d2 = Math.pow(3.5, 1.5);
int i = (int) Math.pow(3.5, 1.5); // require casting for int type
System.out.println("3 raised to the power 5: " + d1);
System.out.println("3.5 raised to the power 1.5: " + d2);
System.out.print("3.5 raised to the power 1.5(for int type): " + i);
This block of code computes the result of 3 raised to the power of 5, 3.5 raised to the power of 1.5, and 3.5 raised to the power of 1.5 for an integer value, and prints out the values to the console.