Its giving me Integer Overflow both in java and C but when I tried it in Python it gave me right answer . Any reasons ?
long long int a = 1000000011/2 * 5 ; printf("%lld",a);
There are two reasons:
- Most computer languages use fixed-size integers. Today, that’s often 32 bits. The correct result of your calculation, 2500000025, is a 32-bit number, meaning it’s too big for a signed 32-bit type. (It comes out as -1794967271 in two’s complement.) Python, on the other hand, is different: it uses arbitrary-precision arithmetic.
- In C, as in many computer languages, expressions are evaluated “inside out”. If you say
float f = 1 / 2;you get 0, because the compiler performs integer division, without looking to see that you wanted to get a floating-point result. If you say
long long int a = 1000000011/2 * 5;you get an overflow, because the compiler performs integer arithmetic, without looking to see that you wanted to get a