Java BigInteger Class | pow() Method with Example

BigInteger Class pow() method: Here, we are going to learn about the pow() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 11, 2020

BigInteger Class pow() method

  • pow() method is available in java.math package.
  • pow() method is used to calculate the pow by using the value of (this BigInteger) is raised to the power of the value of the given parameter (val) [i.e. (this BigInteger) pow (val) ].
  • pow() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • pow() method may throw an exception at the time of calculating power.
    ArithmeticException: This exception may throw when the given parameter holds value less than 0.

Syntax:

    public BigInteger pow(int exp);

Parameter(s):

  • int exp – represents the exponent for this BigInteger.

Return value:

The return type of this method is BigInteger, it returns BigInteger that holds the value [(this BigInteger) pow (exp)].

Example:

// Java program to demonstrate the example 
// of BigInteger pow(int exp) method of BigInteger

import java.math.*;

public class PowOfBI {
    public static void main(String args[]) {
        // Initialize two variables str, exp 
        String str = "10";
        int exp = 2;

        // Initialize a BigInteger object  
        BigInteger b_int = new BigInteger(str);

        // Display b_int , exp
        System.out.println("b_int: " + b_int);
        System.out.println("exp: " + exp);

        // Here, we are calculating pow of this
        // BigInteger value and it is calculated 
        // by using[(this BigInteger (b_int) ^ (exp))]
        // like 10^2 i.e. 100
        BigInteger pow_val = b_int.pow(exp);
        System.out.println("b_int.pow(exp): " + pow_val);
    }
}

Output

b_int: 10
exp: 2
b_int.pow(exp): 100


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.