#include <iostream>
using namespace std;
int main()
{   
    int x=2,a=3,b=2;
    x*=a/b;
    cout<<x<<" ";
    x=2;
    x=x*a/b;
    cout<<x;
    return 0;
}
I am getting output as: 2 3 while in my opinion x*=a/b; and x=x*a/b; mean the same thing. Can anybody explain this behaviour?
They are not quite the same.
x *= a / b is grouped as x *= (a / b) and a / b takes place in integer arithmetic (it's 1).
x = x * a / b is grouped as x = ((x * a) / b). the integer division has a less drastic and different effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With