The java.math.BigInteger.multiply(BigInteger val) is used to calculate the multiplication of two BigIntegers. As BigInteger class internally uses an array of integers for processing, the operation on an object of BigInteger are not as fast as on primitives.
Syntax:
public BigInteger multiply(BigInteger val)
Parameters: This method accepts a parameter val which is the value to be multiplied to this BigInteger.
Return value: This method returns a BigInteger which holds multiplication (this * val).
Below programs is used to illustrate the multiply() method of BigInteger.
Example 1:
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger mult;
String input1 = "012345678901234567"
+ "654632498739473" ;
String input2 = "0123456789012345"
+ "61247612748612746" ;
BigInteger a
= new BigInteger(input1);
BigInteger b
= new BigInteger(input2);
mult = a.multiply(b);
System.out.println( "The multiplication of\n"
+ a + " \nand\n" + b + " "
+ "\nis\n" + mult);
}
}
|
Output:
The multiplication of
12345678901234567654632498739473
and
12345678901234561247612748612746
is
152415787532388282591353462245536419067346861445890674421122858
Example 2:
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger mult;
String input1 = "012345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "0123456789012345"
+ "6789012345678901"
+ "654632498739473" ;
String input2 = "0123456789012345"
+ "6789012345678901"
+ "2345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "61247612748612746" ;
BigInteger a
= new BigInteger(input1);
BigInteger b
= new BigInteger(input2);
mult = a.multiply(b);
System.out.println( "The multiplication of\n"
+ a + " \nand\n" + b + " "
+ "\nis\n" + mult + "\n" );
double d = Double.parseDouble(mult.toString());
System.out.println( "Using double, multiplication is "
+ d);
}
}
|
Output:
The multiplication of
123456789012345678901234567890123456789012345678901234567890123456789012345678901654632498739473
and
123456789012345678901234567890123456789012345678901234567890123456789012345678961247612748612746
is
15241578753238836750495351562566681945008382873376009755225118122311263526910008985036532509972574264073578551235889967606442208008929541925721486305055001841778994861500543809890674421122858
Using double, multiplication is 1.5241578753238838E190
As from the above examples, it is clear that the data is full precise when BigInteger is used.
Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#multiply(java.math.BigInteger)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Apr, 2019
Like Article
Save Article