Jason arora - 6 days ago 6

Java Question

Given two numbers as input, return the sum of the numbers. Note that the numbers can be very large and hence are provided as Strings.

Note: Without using BigInteger class I have to add them.

`#Sample Input #1`

add("2354725234782357","9999999999999999999999999988888888888")

#Sample Output #1

10000000000000000000002354714123671245

#Sample Input #2

add("1600","213")

#Sample Output #2

1813

Since the numbers are very large on which I am not getting output.Can anyone guide what I am doing wrong and How to add these numbers.

`public String add(String str1, String str2) {`

long number1 = Long.parseLong(str1);

long number2 = Long.parseLong(str2);

number1 = number1 + number2;

return "" + number1;

}

Parameters:

`'2354725234782357' '9999999999999999999999999988888888888'`

Actual output:

`null`

Expected output:

`10000000000000000000002354714123671245`

As per MC Emperor suggestions, I got to know how to add numbers very large without using Any BigInteger.

`Below is the code.`

public String add(String a, String b)

{

String result = "";

if(a.length()>b.length())

{

int p=a.length()-b.length();

for(int i=0;i<p;i++)

{

b="0"+b;

}

}

if(b.length()>a.length())

{

int p=b.length()-a.length();

for(int i=0;i<p;i++)

{

a="0"+a;

}

}

if(a.length()==b.length())

{

int carry = 0;

for (int i = a.length() - 1; i >= 0; i--)

{

int digitA = a.charAt(i) - 48;

int digitB = b.charAt(i) - 48;

int resultingNumber = digitA + digitB + carry;

if (resultingNumber >= 10)

{

result = (resultingNumber % 10) + result;

carry = 1;

}

else

{

result = resultingNumber + result;

carry = 0;

}

}

if (carry > 0)

{

result = carry + result;

}

}

return result;

}

}

Answer

You should know that a long has by default 64 bits whose 1 is used for sign, so the maximum value is `2`

, or ^{63}-1`9,223,372,036,854,775,807`

. The assignment is telling you that the numbers can be very large, so I'm guessing that those numbers can exceed the maximum value.

`Long.parseDouble(String)`

doesn't exist, I assume you mean`Long.parseLong(String)`

;- Your code does not output
`null`

as you stated, it rather emits a`NumberFormattingException`

, because the input value is too large.

The assignment is trying to let you build your own mechanism to add two numbers. These are the steps you should take:

- Take two input strings, say
`"134"`

and`"258"`

. You shouldn't convert them to longs, because of the size limit. - Add the two right-most integers, in our case
`4`

and`8`

. If that number is less than 10, you can progress to the next integers. Otherwise, if the number equals or is greater than 10 (in our case, it is`12`

), progress to the next two integers starting from the right (`3`

and`5`

) add 1 to those integers. In our case`3 + 5 + 1 = 9`

. If that number is 10 or greater, add 1 to the next integers. In our case, it isn't. Repeat those steps until you have had all digits of the number.

So in code, you have this:

```
public String add(String a, String b) {
// I assume the strings are equal of length. If they are not, they
// should be padded with zeroes. Otherwise, you can check if one string
// has been 'exhaused', avoiding an IndexOutOfBoundsException.
// The carry is the number which is 'dragged' to the next two integers
// when it is greater than 9. While adding no more than two numbers, the
// carry will always be 0 or 1.
int carry = 0;
// Our result as a string.
String result = "";
// Start at the right-most part the string, because that is
// the least significant number.
for (int i = a.length() - 1; i >= 0; i--) {
// A string is an array of characters, and the characters '0' till
// '9' have the ASCII values of 48 till 57. So if we just subtract
// 48 from the given character, we have the digit the character
// represents.
int digitA = a.charAt(i) - 48;
int digitB = b.charAt(i) - 48;
// Add both digits and the carry.
int resultingNumber = digitA + digitB + carry;
if (resultingNumber >= 10) {
// We only need the last digit, so we're using the modulo
// operator, which calculats the remainder. Then we prepend our
// number to our result string, creating a new result string.
// You should rather use the StringBuilder class, but for
// readability, I didn't.
result = (resultingNumber % 10) + result;
carry = 1;
}
else {
result = resultingNumber + result;
carry = 0;
}
}
// Add the last carry to our result string, if available.
if (carry > 0) {
result = carry + result;
}
return result;
}
```

I have lots of comments added, to explain the code.

*Note that I have also avoided using StringBuilder, and using ternary if-statements, to improve readability.*