0
How its output is 2 but not 3
https://sololearn.com/compiler-playground/cZYpimiQ52zJ/?ref=app
3 Risposte
+ 5
Hi! 5/2 is 2.5, not 3. You're most likely getting the result 2 because you declared your variables a and b as int at the beginning. An int is an integer, and the fractional part is discarded. If you want to get 3, declare the variables as float (double) and round the result up at the end of the calculation. If you want an exact result, declare the variables as floating-point numbers.
+ 4
https://sololearn.com/compiler-playground/clcr05N0JtlC/?ref=app
note that C's round does not follow the standard rounding rule of 'round half to even' for 0.5 (which should round up ony if the preceeeding number is odd). 2.5 rounds to 3.0 instead of 2.0
in python, round is to the nearest even value:
print(round(5/2)) # 2.5 rounded to 2
print(round(7/2)) # 3.5 rounded to 4
round in C, C++ and Go
https://sololearn.com/compiler-playground/cWt2POjvZMee/?ref=app
https://sololearn.com/compiler-playground/cXnGEuCbShU2/?ref=app
+ 3
integer division in C truncates, it does not round.
so 2.1, 2.5, or 2.9 all becomes 2
the numbers after the decimal point are just dropped, they're not rounded.



