+ 1
Need help with division
The Output of the following code is 0.000000. Why? Is there a way to fix this? https://code.sololearn.com/c9u2MQp7t2YE/?ref=app
3 Réponses
+ 3
Binary operators like '/' are overloaded for different data types, if you use two integers as arguments, it assumes you want to do integer division, which results in 0. At least one of the arguments should be of type 'float' for it to work correctly, meaning all you have to do is to cast either 'a' or 'b' to float like this:
sum = (float)a / b;
Here the result will be 0.6, as you would probably expect it.
+ 3
Typecast a or b to float type to get division result in float. Missing semicolon at the end of printf statement.
0
Works perfectly thanks!