Why is (1/2)*100 outputting zero instead of 50

Hi there, Why is this line of code cout << (1/2)*100; displaying 0 instead of 50.
Because 1 and 2 are seen as int. In C++, when two int are used in division, the fractional part is ignored (0.5 --> 0). Try this instead:

 
std::cout << (1.0/2.0) * 100.0;


1.0 and 2.0 will be seen as floats and it will give the wanted result.
Topic archived. No new replies allowed.