In Deitel's C book I read this:
(Rounding Numbers) An application of function floor is rounding a value to the nearest
integer. The statement
y = floor( x + .5 );
will round the number x to the nearest integer and assign the result to y. Write a program that reads
several numbers and uses the preceding statement to round each of these numbers to the nearest
integer. For each number processed, print both the original number and the rounded number.
and I tried to write a program:
Code:
#include <stdio.h>
int main(void) {
float x;
scanf("%f",&x);
printf("%d",floor(x + 0.5));
return 0;
}
but this doesn't work (prints 0 either when x is a floating number or an integer).
What's wrong with my program?
And please explain me how does this kind of rounding work? I don't understand it.
Thanks.