Math operation for BigInteger in java

This example shows you some Math operation using BigInteger object in java.

The BigInteger object provides you various methods to do the math operation on it.

Below is the example of math operation on BigInteger object.

package com.java.connect.math;
 
import java.math.BigInteger;
 
//Math operation for BigInteger in java
public class BigIntegerExample {
    public static void main(String[] args) {
        BigInteger bigInteger1 = new BigInteger("25");
        BigInteger bigInteger2 = new BigInteger("15");
 
        // Addition
        bigInteger1 = bigInteger1.add(bigInteger2);
        System.out.println("Addition : " + bigInteger1);
        // Subtraction
        bigInteger1 = bigInteger1.subtract(bigInteger2);
        System.out.println("subtract : " + bigInteger1);
        // Multiplication
        bigInteger1 = bigInteger1.multiply(bigInteger2);
        System.out.println("Multiply : " + bigInteger1);
        // Divide
        bigInteger1 = bigInteger1.divide(bigInteger2);
        System.out.println("divide : " + bigInteger1);
        // Power
        bigInteger1 = bigInteger1.pow(3);
        System.out.println("power of 3 : " + bigInteger1);
        // Negative
        bigInteger1 = bigInteger1.negate();
        System.out.println("negate : " + bigInteger1);
    }
}

The output of the above example will be as:

Addition : 40
subtract : 25
Multiply : 375
divide : 25
power of 3 : 15625
negate : -15625