Java.math.BigInteger.pow() Method



Description

The java.math.BigInteger.pow(int exponent) returns a BigInteger whose value is (thisexponent). The exponent is an integer rather than a BigInteger.

Declaration

Following is the declaration for java.math.BigInteger.pow() method.

public BigInteger pow(int exponent)

Parameters

exponent − Exponent to which this BigInteger is to be raised.

Return Value

This method returns a BigInteger object whose value is thisexponent.

Exception

ArithmeticException − Exponent is negative. This would cause the operation to yield a non-integer value.

Example

The following example shows the usage of math.BigInteger.pow() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create and assign value to exponent
      int exponent = 2;

      // assign value to bi1
      bi1 = new BigInteger("6");

      // perform pow operation on bi1 using exponent
      bi2 = bi1.pow(exponent);

      String str = "Result is " + bi1 + "^" +exponent+ " = " +bi2;

      // print bi2 value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

Result is 6^2 = 36
java_math_biginteger.htm
Advertisements