why the output of float a=1/2; is 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why the output of float a=1/2; is 0?

15th Nov 2019, 1:55 PM
Bashir Uddin
Bashir Uddin - avatar
2 Answers
+ 4
Because 1 and 2 are integer values so it gives an integer output. You need to typecast it to get a float output. For eg- float a = (float) 1/2; OR float a = 1.0f/2.0f;
15th Nov 2019, 2:20 PM
Avinesh
Avinesh - avatar
+ 4
or simply float a = 1/2.0; will do
15th Nov 2019, 3:51 PM
Rohit