C# modulus operator

霸气de小男生 提交于 2019-11-27 22:47:54
NullUserException

Because the remainder of 3 / 4 = 3.

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

If you can't figure out why the remainder is 3, we've got some more serious problems here.

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

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.

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

Rehan Manzoor

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

e.g.

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

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

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
}

I found the answer confusing and misleading.....

Modulus is what is left over IN THE FIRST NUMBER after dividing the second 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!