Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic operator confusion

Tags:

java

operators

Why I'm getting two different values while using the arithmetic operators for the same value of variables. I've just altered little bit my second program, which is resulted in giving me the different output. Could anyone please tell me why?

    int number=113;
 int rot=0;
 rot=number%10;
 rot*=100+number/10;
 System.out.println(rot);//333



    int number=113;
 int rot=0;
 rot=number%10;
 rot=rot*100+number/10;
 System.out.println(rot);//311
like image 891
Dusk Avatar asked Jul 06 '26 13:07

Dusk


1 Answers

In the first part you compute

rot *= 100 + number/10

which is

rot = rot * (100 + number/10)

And in the second part:

rot = rot*100 + number/10

Note that multiplication and division goes before addition and substraction.

like image 178
Felix Kling Avatar answered Jul 08 '26 02:07

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!