# modulus operator / '%' question

Printable View

• 01-17-2006
musikluvah
modulus operator / '%' question
Gosh this is such a simple question but dont quite understand the logic. I understand that the modulus operator is represented by '%'. And I understand that the expression x % y produces the remainder when x is divided by y.

So when I divide 74 by 5 the answer is 14.8 . So I thought I should see the answer 8 but instead I see 4. Somebody please explain to me why there is a 4 instead of 8?

[output]

4

[/output]

Code:

``` #include <stdio.h> main() {   int it;   it = 74 % 5;     printf("%d\n", it);   }```
• 01-17-2006
filker0
The % operator is defined as the integer remainder (modulus) of integer division.

If you have integers A and B, where C = int(A / B), you can see the remainder, R, as being the result of the expression R = A - (B * C).

What you were expecting was not the remainder, but the fractional part of a floating point result of a division between two whole numbers. That's not what % gives you.
• 01-17-2006
Salem
Because modulus is the remainder of division, not a fraction.
But 0.8 is also 4 / 5

modulus = x - ( (x / y) * y ), when performed in integer arithmetic.
• 01-17-2006
grib
(a/b)*b + a % b = a
Or with decimals
14.8 is equal to 14 and eight tenths, eight is not the remainder of anything.
Eight tenths is equal to 4/5, that is the remander(4 or 74 % 5) divided by 5.
For integers divided by a power of 10 the numbers after the decimal point are the remainder
74 / 10 = 7.4
74 % 10 = 4
• 01-17-2006
musikluvah
got it - thank you.