Power Method of Math Class in Java


Introduction

Returns the value of the first argument raised to the power of the second argument in Java.

double pow(double base, double exponent)

Sample Code

public class main {
public static void main (String [] args) {

//Returns the value of the first argument raised to the power of the second argument.
System.out.println("Math.pow(2,0) = " + Math.pow(2, 0));
System.out.println("Math.pow(2,1) = " + Math.pow(2, 1));
System.out.println("Math.pow(2,2) = " + Math.pow(2, 2));
System.out.println("Math.pow(2,3) = " + Math.pow(2, 3));
System.out.println("Math.pow(2,4) = " + Math.pow(2, 4));
System.out.println("Math.pow(2,5) = " + Math.pow(2, 5));
System.out.println("Math.pow(2,6) = " + Math.pow(2, 6));
System.out.println("Math.pow(2,7) = " + Math.pow(2, 7));
System.out.println("Math.pow(2,8) = " + Math.pow(2, 8));
System.out.println("Math.pow(2,9) = " + Math.pow(2, 9));
System.out.println("Math.pow(2,10) = " + Math.pow(2, 10));
}
}

Output from The Program

Math.pow(2,0) = 1.0
Math.pow(2,1) = 2.0
Math.pow(2,2) = 4.0
Math.pow(2,3) = 8.0
Math.pow(2,4) = 16.0
Math.pow(2,5) = 32.0
Math.pow(2,6) = 64.0
Math.pow(2,7) = 128.0
Math.pow(2,8) = 256.0
Math.pow(2,9) = 512.0
Math.pow(2,10) = 1024.0

Java Tutorial Beyond Basic Arithmetic

張貼留言

0 留言