0

This code- (8+4)/2 displays6.0 but not 6.why?

4th Jan 2018, 7:21 AM
Chinmay Khandelwal
Chinmay Khandelwal - avatar
3 Answers
+ 12
Division on integers creates a float result, you can convert (or cast) the result into integer for getting the desired output. int((8+4)/2)
4th Jan 2018, 7:25 AM
Dev
Dev - avatar
+ 9
I am not getting 6.0 when I do (8+4)/2, but it may be that the division operation is returning a float in your case. So, to return integers, use floor division (//) or cast the return type to int. Eg : >>> (8+4)//2 6 >>> int((8+4)/2) 6
4th Jan 2018, 7:26 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 8
(8+4)2 gives me 6 here not 6.0,not sure what your case, but use int() to convert and to integer
4th Jan 2018, 7:31 AM
᠌᠌Code X
᠌᠌Code X - avatar