Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sum of two large numbers which can't be held in any primitive datatype?

Tags:

java

How to find sum of two large numbers which can't be held in any primitive datatype??

like image 601
nick Avatar asked May 30 '12 13:05

nick


People also ask

How do you add two large numbers in Java?

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.

How do you add a large number in Python?

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.

How do you add a large integer in C++?

Step 1.1: intSum = number1[i] + number2[i] Step 1.2: carry = intSum/10. Sum += intSum Step 2: sum += carry. Step 3: return sum.


2 Answers

I assume by "any datatype" you mean "any primitive datatype".

Depending on the type of your data, you could use BigInteger or BigDecimal.

like image 117
NPE Avatar answered Nov 03 '22 01:11

NPE


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.

like image 20
wattostudios Avatar answered Nov 03 '22 01:11

wattostudios