C# modulus operator

Ben Ziegler picture Ben Ziegler · Aug 6, 2010 · Viewed 94.9k times · Source

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.

Answer

quantumSoup picture quantumSoup · Aug 6, 2010

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. :)