Multiply one BigInteger to another BigInteger in Java



BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

To multiply one BigInteger to another, use the BigInteger multiply() method.

First, let us create some objects −

BigInteger one, two, three;
one = new BigInteger("2");
two = new BigInteger("8");

Multiply the above and assign it to the third object −

three = one.multiply(two);

The following is an example −

Example

 Live Demo

import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      BigInteger one, two, three;
      one = new BigInteger("2");
      two = new BigInteger("8");
      three = one.multiply(two);
      String res = one + " * " + two + " = " +three;
      System.out.println("Multiplication: " +res);
   }
}

Output

Multiplication: 2 * 8 = 16
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements