Skip to content
Related Articles

Related Articles

BigInteger pow() Method in Java
  • Last Updated : 12 Apr, 2019

The java.math.BigInteger.pow(int exponent) method is used to calculate a BigInteger raise to the power of some other number passed as exponent whose value is equal to (this)exponent. This method performs operation upon the current BigInteger by which this method is called and exponent passed as parameter.
Syntax:

public BigInteger pow(int exponent)

Parameter: This method accepts a parameter exponent which is the exponent to which this BigInteger should be raised to.
Returns:
This method returns a BigInteger which is equal to (this)exponent.
Exception:
The parameter exponent must be positive number (exponent >= 0) otherwise ArithmeticException is thrown.
Examples:

Input: BigInteger1=321456, exponenet=5
Output: 3432477361331488865859403776
Explanation: BigInteger1.pow(exponent)=3432477361331488865859403776. 
321456^5=3432477361331488865859403776

Input: BigInteger1=45321, exponenet=3
Output: 93089018611161
Explanation: BigInteger1.pow(exponent)=93089018611161. 
321456^5=93089018611161

Below programs illustrate pow() method of BigInteger class
Example 1:




// Java program to demonstrate 
// pow() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating BigInteger object
        BigInteger b1;
  
        b1 = new BigInteger("321456");
        int exponent = 5;
  
        // apply pow() method
        BigInteger result = b1.pow(exponent);
  
        // print result
        System.out.println("Result of pow operation between BigInteger " 
             + b1 + " and exponent " + exponent + " equal to " + result);
    }
}

Output:

Result of pow operation between BigInteger 321456 and exponent 5 equal
to 3432477361331488865859403776



Example 2:




// Java program to demonstrate
// pow() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating BigInteger object
        BigInteger b1;
  
        b1 = new BigInteger("12346");
        int exponent = 6;
  
        // apply pow() method
        BigInteger result = b1.pow(exponent);
  
        // print result
        System.out.println("Result of pow operation between BigInteger "
             + b1 + " and exponent " + exponent + " equal to " + result);
    }
}

Output:

Result of pow operation between BigInteger 41432345678 and exponent 6 equal to 5058679076487529899393537031261743031889730764186441745527485504

Example 3:
Program showing exception when exponent passed as parameter is less than zero.




// Java program to demonstrate 
// pow() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating BigInteger object
        BigInteger b1;
  
        b1 = new BigInteger("76543");
        int exponent = -17;
          
        try {
    
        // apply pow() method
        BigInteger result = b1.pow(exponent);
  
        // print result
        System.out.println("Result of pow operation between " + b1
                           + " and " + exponent + " equal to " + result);
          
        catch (Exception e) { 
            System.out.println(e); 
        }
  
    }
}

Output:

java.lang.ArithmeticException: Negative exponent

Reference:https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#pow(int)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :