What does the% operator mean?

I have the following code

int a,b,c; b=1; c=36; a=b%c; 

What does the% operator mean?

+6
operators c #
source share
11 answers

This is a module (or module) :

The module operator (%) calculates the remainder after dividing its first operand by its second.

For example:

 class Program { static void Main() { Console.WriteLine(5 % 2); // int Console.WriteLine(-5 % 2); // int Console.WriteLine(5.0 % 2.2); // double Console.WriteLine(5.0m % 2.2m); // decimal Console.WriteLine(-5.2 % 2.0); // double } } 

Output Example:

  one
 -one
 0.6
 0.6
 -1.2

Note that the result of the % operator is x – (x / y) * y and that if y is zero, then a DivideByZeroException .

If x and y are non-integer values x % y , it is calculated as x – n * y , where n is the largest possible integer that is less than or equal to x / y (for more information see Specification C # 4.0 in Section 7.8.3 Stop Operator )

For more information and examples, you can read the related Wikipedia article:

Work with the module (on Wikipedia)

+26
source share

This is the Modulo operator. This will give you the remainder of the division operation.

+6
source share

% is the remainder operator in many languages ​​that support C.

 3 % 2 == 1 789 % 10 = 9 

This is a little tricky with negative numbers. In particular. Java and C #, the result has the same sign as the dividend:

 -1 % 2 == -1 

In particular. C ++ is an implementation defined.

see also

References

+5
source share

This is the module operator. That is, 2% 2 == 0, 4% 4% 2 == 0 (2, 4 are divided by 2 with a remainder of 0), 5% 2 == 1 (2 goes into 5 with 1 as a remainder.)

+3
source share

This is the modulo operator. those. residue after division 1 % 36 == 1 (0 residue 1)

+2
source share

This is the modulo operator, which finds the remainder of dividing one number by another.

So, in this case, a will be the remainder of b divided by c .

+1
source share

This is a module, but you, for example, do not use it very well. This gives you the remainder when two integers are separated.

eg. a = 7 % 3 will return 1, because 7 divided by 3 is 2 with the remaining 1.

+1
source share

This is a modular operator.

 using System; class Test { static void Main() { int a = 2; int b = 6; int c = 12; int d = 5; Console.WriteLine(b % a); Console.WriteLine(c % d); Console.Read(); } } 

Output:

 0 2 
0
source share

is a basic operator available in almost every language and commonly known as the modulo operator. he gives the result as a result.

0
source share

Well, I knew about it, until I just tried the calculator and played like this basically:
5 % 2.2 = 0.6 is how to say it in the calculator 5/2.2 = 2.27 , then you multiply this .27 times by 2.27 , and you round, and you get 0.6 . Hope this helps, it helps me =]


0
source share

No one here has presented any examples of exactly how an equation can return different results, for example, comparing 37/6 with 37%6 , and before some of you are upset thinking you did it, stop for a moment and think about it for a minute, according to Dirk Folmar, here int x % int y analyzes as (x - (x / y) * y) , which seems pretty simple at first glance, but not all Math is executed in the same order.

Since each equation does not have the right brackets, some schools will explain that the equation should be analyzed as ((x - (x / y)) * y) , while other schools will teach (x - ((x / y) * y)) .

Now I was experimenting with my example ( 37/6 and 37%6 ) and figured out what choice was intended (it (x - ((x / y) * y)) ), and I even showed a beautifully constructed if loop (although I forgot each End of the linear semicolon) to simulate the separation equation in the most fundamental scale, since this was actually my point, the equation is similar, but fundamentally different. Here's what I can remember from my remote Mail (it took me around the Hour to print from my phone, indentation with double spacing)

 using System; class Test { static void Main() { float exact0 = (37 / 6); //6.1666e∞ float exact1 = (37 % 6); //1 float exact2 = (37 - (37 / 6) * 6); //0 float exact3 = ((37 - (37 / 6)) * 6); //0 float exact4 = (37 - ((37 / 6) * 6)); //185 int a = 37; int b = 6; int c = 0; int d = a; int e = b; string Answer0 = ""; string Answer1 = ""; string Answer2 = ""; string Answer0Alt = ""; string Answer1Alt = ""; string Answer2Alt = ""; Console.WriteLine("37/6: " + exact0); Console.WriteLine("37%6: " + exact1); Console.WriteLine("(37 - (37 / 6) * 6): " + exact2); Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3); Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4); Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e); Console.WriteLine("Answer0: " + Answer0); Console.WriteLine("Answer0Alt: " + Answer0Alt); Console.WriteLine("Answer1: " + Answer1); Console.WriteLine("Answer0Alt: " + Answer1Alt); Console.WriteLine("Answer2: " + Answer2); Console.WriteLine("Answer2Alt: " + Answer2Alt); Console.WriteLine("Init Complete, starting Math..."); Loop { if (a !< b) { a - b; c +1;} if else (a = b) { a - b; c +1;} else { String Answer0 = c + "." + a; //6.1 //this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1, //which according to my Calculator App is technically correct once you Round Down the .666e∞ //which has been stated as the default behavior of the C# / Operand //for completion sake I'll include the alternative answer for Round Up also String Answer0Alt = c + "." + (a + 1); //6.2 Console.WriteLine("Division Complete, Continuing..."); Break } } string Answer1 = ((d - (Answer0)) * e); //185.4 string Answer1Alt = ((d - (Answer0Alt​)) * e); // 184.8 string Answer2 = (d - ((Answer0) * e)); //0.4 string Answer2Alt = (d - ((Answer0Alt​) * e)); //-0.2 Console.WriteLine("Math Complete, Summarizing..."); Console.WriteLine("37/6: " + exact0); Console.WriteLine("37%6: " + exact1); Console.WriteLine("(37 - (37 / 6) * 6): " + exact2); Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3); Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4); Console.WriteLine("Answer0: " + Answer0); Console.WriteLine("Answer0Alt: " + Answer0Alt); Console.WriteLine("Answer1: " + Answer1); Console.WriteLine("Answer0Alt: " + Answer1Alt); Console.WriteLine("Answer2: " + Answer2); Console.WriteLine("Answer2Alt: " + Answer2Alt); Console.Read(); } } 

It also PURE demonstrated how the result may differ for the same equation.

-3
source share

All Articles