0

Multiplication of 123456789 and 987654321 is yielding in -67153019. I have used both int and long types but couldn't find the required result.

int n1 = 123456789 ;
int n2 = 987654321 ;
long ans = n1*n2 ;

The result should be 121932631112635269 but it comes out to be -67153019.

4
  • 1
    You forgot to ask a question. Please edit the post and as a focused question. --- n1*n2 is still an int-expression. I recommend to either define n1 and n2 as long or cast one of the operatnds explicitly to long. Commented Jan 15, 2023 at 19:23
  • long and int has upper and lower limit and when the result of the operation you make is over the limit for your case it will overflow and will start count from the opsite side which is the negative in your case for very huge number use BigInteger docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/… but to be more specfic for your case when you do operation using both int then this operation will be int * int then result will convert to long in your case and since the result is over the int limit that what you get Commented Jan 15, 2023 at 19:27
  • Gee. The compiler wars about so many things. But in this case neither it warns nor is there some notification at runtime. I am surprised. Commented Jan 15, 2023 at 19:27
  • @Queeg why should it warn? There is nothing to warn about. n1*n2 is an int-expression, and the convertion from int to long is widening, so there is not loss int he conversion. Commented Jan 15, 2023 at 19:28

2 Answers 2

2

This is because 121932631112635269 is greater than max int, so when doing n1 * n2, the result parsed to int will be garbage. The cast to Long is made after the math, when it tries to do the = with ans.

If you cast n1 and n2 to long before, doing the math you will get 121932631112635269.

    int n1 = 123456789 ;
    int n2 = 987654321 ;
    long ans = (long)n1 * (long)n2;

or like this:

    int n1 = 123456789 ;
    int n2 = 987654321 ;
    long ans = 1L * n1 * n2;

or if you change n1 and n2 types to long

edit: as Turing85 says,

the 2nd cast is superfluous

since the n1 would be cast to long before, there's no need to cast n2, since the result would be a Long. Making:

int n1 = 123456789 ;
int n2 = 987654321 ;
long ans = (long)n1 * n2;

also correct

Sign up to request clarification or add additional context in comments.

4 Comments

"ong ans = (long)n1 * (long)n2;" - The 1st case is superfluous. The multiplication has higher precedence than the cast.
not really, it casts to long before doing the multiplication
huh... interesteing... then the 2nd cast is superfluous.
You are right, the second cast is superflous since the result of the multiplication will be a long since first element is a long.
0

It is due to order of operations. In your case, first JVM tries to multiply 2 ints as an int (because result of binary operation with int operands is also an int), and only then puts the result to long because the left-hand side variable has long type.

Please try this:

long ans = 1L * n1 * n2;

This should solve the overflow issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.