Calculate the power on a BigInteger in Java

Java 8Object Oriented ProgrammingProgramming

Use the BigInteger pow() method in Java to calculate the power on a BigInteger.

First, let us create some objects.

BigInteger one, two;
one = new BigInteger("5");

Perform the power operation and assign it to the second object −

// power operation
two = one.pow(3);

The following is an example −

Example

 Live Demo

import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      BigInteger one, two;
      one = new BigInteger("5");
      System.out.println("Actual Value: " +one);
      // power operation
      two = one.pow(3);
      System.out.println("Result: " +two);
   }
}

Output

Actual Value: 5
Negated Value: 125
raja
Published on 20-Dec-2018 06:15:00
Advertisements