BigInteger pow() Method in Java

The java.math.BigInteger.pow(int exponent) method returns a BigInteger whose value is equal to (this ^ exponent) or (this ** exponent).This function is used to calculate a BigInteger raise to the power of some other number passed as exponent.
Syntax:

public BigInteger pow(int exponent)

Parameter:
exponent- the exponent to which we want to raise power to BigInteger.
Returns:
returns the BigInteger which is equal to this**exponent.
throws
ArithmeticException – big < 0
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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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, b2;
  
        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);
    }
}

chevron_right


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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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, b2;
  
        b1 = new BigInteger("12346");
        int exponent = -4;
  
        // 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);
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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, b2;
  
        b1 = new BigInteger("76543");
        b2 = new BigInteger("-17");
  
        // apply pow() method
        BigInteger result = b1.pow(b2);
  
        // print result
        System.out.println("Result of pow operation between " + b1
                           + " and " + b2 + " equal to " + result);
    }
}

chevron_right


Output:
Exception in thread "main" java.lang.ArithmeticException: Negative exponent
    at java.math.BigInteger.pow(BigInteger.java:2213)
    at GFG.main(GFG.java:16)

Reference:
BigInteger pow() Docs



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.