How to get the sum of two large numbers that cannot be held in any primitive data type?

How to find the sum of two large numbers that cannot be stored in any primitive data type?

+5
source share
3 answers

I assume that "any data type" means "any primitive data type."

Depending on the type of your data, you can use BigIntegeror BigDecimal.

+12
source

You can perform the calculation manually, saving 2 numbers as Strings(or an array char[]), and then doing the repetition with char-by- char.

+3
source
import java.math.BigInteger;

  BigInteger n1 = new BigInteger("123456789012345678901234567890");
  BigInteger n2 = new BigInteger("111122222233334445556666");
  BigInteger result = n1.add(n2);
  System.out.println(result.toString());
0
source

All Articles