I have a very long number for example "54545455454545454555". And i want to add it with another long number like "7878787878788878787". But the problem is that the range of this number is so long and greater than the ranges of data types like int, double or float. Please tell me the method how to add these two numbers.
How to add two long numbers in java
In Java library you will find BigInteger (for integers) and BigDecimal (for numbers with decimal digits). Both classes are in Java.math package and it supports mathematics operations such addition, multiplication or even more complex operations. It supports values that are greater than the standard data types.
The best solution is to go to BigIntergers for long numbers. (https://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html).
Another solution is GMP (gmplib.org) which is a high-performance multi-precision library and to use it in Java you need to have JNI wrappers around the binary library.
How to add two long numbers in java
Hi Afsheen,
You can use Big Integer library in Java. What you need to do is create instants of the class and assign the long integer to it. For example
BigIntegernum1 =newBigInteger("54545455454545454555");
BigIntegernum2 =newBigInteger("7878787878788878787");
BigIntegerresult =num1.add(num2);
Here I assigned your numbers to num1 and num2 and called the function add().The BigInteger will let you work with numbers of any size.
Hope this help!