45

I can write the program

int a = 3;
int b = 4;

Console.WriteLine(a % b);

The answer I get is 3. How does 3 mod 4 = 3???

I can't figure out how this is getting computed this way.

Jonathan Nixon
  • 4,940
  • 5
  • 39
  • 53
Ben Ziegler
  • 895
  • 1
  • 7
  • 18
  • 34
    The result is correct. What were you expecting it to return? – Alfred Myers Aug 06 '10 at 20:38
  • 5
    You were thinking of b%a not a%b. – Jimmy Hoffa Aug 06 '10 at 20:39
  • 1
    I wasn't quite sure what to expect, but I couldn't figure out how the remainder was 3. – Ben Ziegler Aug 06 '10 at 20:40
  • 1
    Is it a modulus operator or a remainder operator? They differ when the divisor is negative. Specifically, both compute `r` in `D = dq + r`, but modulus rounds `d` towards minus infinity, while remainder rounds `d` towards zero. – isekaijin Dec 17 '13 at 01:44
  • zero 4s divide into 3 and remainder is 3 – Jodrell Sep 22 '22 at 11:56
  • Modulo returns the remainder. 3 does not divide evenly (or without a remainder) into 4. To state the obvious, you need at least 4. Therefore, if you can make at least 1 whole division, then 3 remains. – Adam Cox May 03 '23 at 01:47
  • Voting to close as per [What to do with "programming" Qs that are actually just basic arithmetic/ratio questions?](https://meta.stackoverflow.com/q/316914/5532169) – zypA13510 Jun 05 '23 at 19:30

8 Answers8

133

I wasn't quite sure what to expect, but I couldn't figure out how the remainder was 3.

So you have 3 cookies, and you want to divide them equally between 4 people.

Because there are more people than cookies, nobody gets a cookie (quotient = 0) and you've got a remainder of 3 cookies for yourself. :)

quantumSoup
  • 27,197
  • 9
  • 43
  • 57
74

Because the remainder of 3 / 4 = 3.

http://en.wikipedia.org/wiki/Modulo_operator

user229044
  • 232,980
  • 40
  • 330
  • 338
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • That's very funny indeed. However, you can extend the math problem by adding a decimal point to the top and more zeros to the dividend to get it to eventually go evenly which would equal .75 as a quotient and no remainder. I didn't know the mod operator wouldn't do that, hence why I asked the question. – Ben Ziegler Aug 06 '10 at 20:46
  • 4
    .75 is not a quotient. By definition the quotient is an integer. – quantumSoup Aug 06 '10 at 20:50
  • 2
    Think only integers, not decimals – Lost in Alabama Aug 06 '10 at 20:52
  • @Ben, `4` does not evenly go into `3` 0.75 times. That's sort of the point. – Anthony Pegram Aug 06 '10 at 20:52
  • 2
    I was thinking decimals and I shouldn't have been. Thank you all for your responses, they have been most helpful. – Ben Ziegler Aug 06 '10 at 20:54
  • Just a note for anyone else that might be looking at this - if you allow for decimals you will never have a remainder, and the modulus operator is useless. In this case, 3 / 4 = 0 r3 – Jesse Williams Sep 30 '15 at 17:04
  • The modulus operator returns the NUMERATOR of the fraction that is left over after the division is done. – David Klempfner Jun 03 '20 at 06:36
19

3 mod 4 is the remainder when 3 is divided by 4.

In this case, 4 goes into 3 zero times with a remainder of 3.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
6

I found the accepted answer confusing and misleading.

Modulus is NOT the same as modern division that returns a ratio of dividend and divisor. This is often expressed as a decimal quotient that includes the remainder in the quotient. That is what trips people up.

Modulus is just the remainder in division before its used in a decimal quotient.

Example: The division of two numbers is often expressed as a decimal number (quotient). But the result of the division of say, 1/3, can also be expressed in whole numbers as "0 with a remainder of 1". But that form of quotient is not very helpful in modern math, so a decimal value or the ratio of the two numbers is often what we see returned in modern calculators.

1 / 3 = .333333333......

But modulus does not work that way. It ignores the decimal quotient value or ratio returned from division, takes the quotient expression of "0 with a remainder of 1" in 1/3, and extracts the 1 or remainder that was returned from that division. It just strips out the remainder from the quotient and spits back the remainder of division before its converted to a decimal. Below is how modulus works...

1 % 3 = 1

As such, a better way of describing Modulus is to say it is what is left over as an integer (remainder) in the first number (dividend) after dividing the second number (divisor) into it as many times as possible.

1 % 1 = 0 because after dividing 1 into 1, one time, there's nothing left
2 % 1 = 0 because after dividing 1 into 2, two times, there's nothing left
1 % 2 = 1 because 2 won't go into 1, so 1 is left

These whole number remainders returned by modulus (modular math) are useful in software programs in extracting the day of a week, creating alternating sequences, finding if a number is even or odd, etc.

Stokely
  • 12,444
  • 2
  • 35
  • 23
4

I already think that the user may have understood the answers. Because there are so many good programmer.. in simple wording % tells you the reminder after dividing with your own integer.

e.g.

int a = int.Parse(Console.ReadLine());
int b = a % 2;

Now your input 13, it will give 1, because after diving 13 by 2 remainder is 1 in simple mathematics. Hope you got that.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
2

As explained by others, but if you don't want to use the "mod" operator. Here is the equation to figure out the remainder of "a" divided by "n"

a-(n* int(a/n))

ajay
  • 21
  • 1
0

Another "as explained by others", but if you're curious about several more ways to do modulus (or use an alternative method), you can read this article which benchmarks a few different ways.

Basically, the fastest way is the good old fashioned modulus operator, similar to:

if (x % threshold == some_value)
{
    //do whatever you need to
}
0

Perhaps the C# implementation is self explanatory -

static void Main(string[] args)
{
    int a = 3;
    int b = 4;
    Console.WriteLine(a % b);
    Console.WriteLine(MOD(a,b));
    Console.ReadKey();
}
public static int MOD(int a, int b)
{
    int i, j, k;

    i = a / b;
    j = i * b;
    k = a - j;

    return k;
}
user10186832
  • 423
  • 1
  • 9
  • 17