+ 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

15th Mar 2019, 1:39 PM
Julian Dörling
Julian Dörling - avatar
3 Respostas
+ 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.
15th Mar 2019, 1:49 PM
Shadow
Shadow - avatar
+ 3
Typecast a or b to float type to get division result in float. Missing semicolon at the end of printf statement.
15th Mar 2019, 1:53 PM
Шащи Ранжан
Шащи Ранжан - avatar
0
Works perfectly thanks!
15th Mar 2019, 1:57 PM
Julian Dörling
Julian Dörling - avatar