How to find sum of two large numbers which can't be held in any primitive datatype??
add(BigInteger val) is used to calculate the Arithmetic sum of two BigIntegers. This method is used to find arithmetic addition of large numbers of range much greater than the range of biggest data type double of java without compromising with the precision of the result.
You can add/subtract large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.
Step 1.1: intSum = number1[i] + number2[i] Step 1.2: carry = intSum/10. Sum += intSum Step 2: sum += carry. Step 3: return sum.
I assume by "any datatype" you mean "any primitive datatype".
Depending on the type of your data, you could use BigInteger
or BigDecimal
.
You could perform the calculation manually by storing the 2 numbers as Strings
(or a char[]
array) and then iterating through char
-by-char
performing the addition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With