# How, exactly, does the % operator work?

 0 I'm solving some Euler problems, and the % operator is very useful. I take it to mean 'false is a number is divisible by n', or: ``````if(6 % 2){ //won't be done ``````So what's the opposite of it? Some variant of !% more ▼ asked Jan 09 '12 at 10:54 PM Muzz5 1.2k ● 65 ● 83 ● 94 Lo0NuhtiK 3.5k ● 1 ● 9 ● 39 also just look for C# on Google. You will usually find a link to Microsoft where they explain the language of their own. Jan 09 '12 at 11:53 PM BerggreenDK add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 It's called the Modulus operator, and it returns the remainder of an equation. Ex: ``````9 % 2 = 1 `````` Check the docs for a more in depth example: http://msdn.microsoft.com/en-us/library/h6zfzfy7%28v=vs.80%29.aspx The opposite would be a division operator applied to integers, thus ignoring the remainder. Ex: ``(int)9 / (int)2 = 4`` more ▼ answered Jan 09 '12 at 11:04 PM karl_ 2.4k ● 41 ● 53 ● 69 Nice answer, thanks, upvote. Jan 10 '12 at 10:58 AM Muzz5 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Well as they above explain, its a remainder. ``````if(6 % 2) { // 6 divided by 2 and the remainder is 0, because 6/3 is 2. } ``````Actually, this is not valid in C#. So you're probably using UnityScript and unity script converts this into something like `if((6%2)!=0)`That being said: If you use modulo, the correct way of doing it is ``````if((6 % 2) == 0)) // will enter if((6 % 2) != 0)) // will not enter ``````or ``````int remain = 7 % 2; // remain will be 1, because 6 dived through 2 is 3, 1 remains if(remain!=0) if(remain==0) // depending what you want `````` more ▼ answered Jan 10 '12 at 02:24 AM Tseng 1.5k ● 6 ● 8 ● 23 I would mark your answer right, as it's got some good examples, but the other one had more votes, so I'll just upvote yours. Jan 10 '12 at 10:59 AM Muzz5 You vote the answer which is right for you, not which other have chosen or voted up. It's not democratic hehe :) Jan 12 '12 at 08:50 PM Tseng add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 I think there is not really an opposite of %, because x%y mean a number (except when y=0 - division by zero error). 6!%2 for example, does not make too much sense for me (looks like 'if is not possible to divide 6 with 2'). The simplest solution for you to use % is: ``````if(n%y== z) { //won't be done } else { //well, done } `````` Actually: ``````if(n%2== 0) { // Divisible by 2 //won't be done } else { // opposite //well, done } `````` more ▼ answered Jan 10 '12 at 12:11 AM nastasache 188 ● 1 ● 3 ● 5 Please don't provide this kind of code behavior/pattern. It's plain wrong. The expression inside of `if(...)` must always be a boolean (`true` or `false`), nothing else. Some languages (JavaScript, PHP, Unityscript obviously too) will lead to unexpected behavior, like in the example above. Jan 10 '12 at 02:27 AM Tseng @Tseng Ahh, Thx! My inadvertently. I have it edited. if(n%2) = 0 to if(n%2== 0) Jan 10 '12 at 09:59 AM nastasache add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email: