Well, if a compilation error happened, you won't be able to run your program at all because it won't compile (the executable ./remainder
in your case won't exist)!
So this has nothing to do with the compiler.
Let's walk through your code to see what's really happening typically
You get a
float
value from the user and store it inf
(e.g.,9.73
).You multiply this value by
100
and store the result inn
. Notice that becausen
is anint
, all the digits after the decimal points are truncated. Son = 9.73 * 100 = 973
You then you divide
n
by25
and storing the result ini
. Soi = 973 / 25 = 38
Finally, you're taking the remainder of dividing
n
by25
and storing the result inn
. So973 / 25 = 38 38 * 25 = 950 973 - 950 = 23
and you're printing the value of i
and n
respectively which should be 38
and 23
.
But what really happened?
The problem is that float
s are NOT precise. This means thatMeanwhile, when you enter thea value like 9.73
, it isn't necessarily stored as 9.73
. It can
In fact, it may be stored as 9.72999954
(depends on many things). And since you're storing it in an int
, you get 972
stored at the end (as the digits after the decimal points are truncated). And you
You then play with thisthe 972
all the way down through your code! And that explains why you get 22
instead of 23
.