Arithmetic operations with BigInteger

BigInteger Arithemetic operations

Program

import java.math.BigInteger;

public class BigIntegerArithmeticDemo {
    public static void main(String[] args) {
        BigInteger a = new BigInteger("100");
        BigInteger b = new BigInteger("30");

        System.out.println("a = " + a);
        System.out.println("b = " + b);

        System.out.println("Addition: " + a.add(b));              // 130
        System.out.println("Subtraction: " + a.subtract(b));      // 70
        System.out.println("Multiplication: " + a.multiply(b));   // 3000
        System.out.println("Division: " + a.divide(b));           // 3
        System.out.println("Remainder: " + a.remainder(b));       // 10

        BigInteger[] result = a.divideAndRemainder(b);
        System.out.println("Quotient: " + result[0]);             // 3
        System.out.println("Remainder: " + result[1]);            // 10

        System.out.println("Power: " + b.pow(3));                 // 27000
        System.out.println("GCD: " + a.gcd(b));                   // 10

        System.out.println("Negation of a: " + a.negate());       // -100
        System.out.println("Absolute value of -b: " + b.negate().abs()); // 30
    }
}
Scroll to Top