【发布时间】:2022-12-27 20:32:16
【问题描述】:
Question is:
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
Code:
class Solution {
public String multiply(String num1, String num2) {
long n1=0, n2=0, res;
n1 = Long.parseLong(num1);
n2 = Long.parseLong(num2);
res = n1 * n2;
String str = Long.toString(res);
return str;
}
}
Question is:
Its working properly when I give smaller number is like:
Input :40, 90
Output:3600
Input :100, 2099
Output:209900
If i give input like this:
Input :498828660196, 840477629533
Output:"-3269442614257959980"
But the Actual output is : 419254329864656431168468. I don't know why answer coming like this. am also using long datatype. Anyone explain me and give solution for this problem.
【问题讨论】:
-
it is called overflow, more here baeldung.com/java-overflow-underflow