9

I have next code

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

What does "%" operator mean?

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
Polaris
  • 3,643
  • 10
  • 50
  • 61
  • 5
    **modulo,** or remainder after division. – Mark H Jul 16 '10 at 11:54
  • 3
    be aware that this operator exist in almost every language. – mathk Jul 16 '10 at 11:58
  • 1
    Yes I do. But I can search it like "%" operator and google didn't give any useful page. I didn't know that it named "modulus" – Polaris Jul 16 '10 at 12:08
  • 4
    @Incognito: Operators are usually not that easy to search for... – Dirk Vollmar Jul 16 '10 at 12:13
  • Another, often overlooked, place to search these days is a thing called a 'book'. I have several C# books and a quick look in the index does indeed show '%'. It's not that difficult. – Simon Knights Jul 16 '10 at 12:31
  • 1
    @0xA3: Search "C# operators" in Google and % is in the first row of the first page that pops up. – Justin Ardini Jul 16 '10 at 12:35
  • Note that there's a difference between the mathematical and computer's definition of modulus. In mathematics, modulus is always non-negative, but this is not so in computer science (-5 % 2 == -1). – apoorv020 Jul 16 '10 at 12:56

11 Answers11

29

It is the modulo (or modulus) operator:

The modulus operator (%) computes 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
    }
}

Sample output:

1
-1
0.6
0.6
-1.2

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

If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).

For further details and examples you might want to have a look at the corresponding Wikipedia article:

Modulo operation (on Wikipedia)

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
7

% is the remainder operator in many C-inspired languages.

3 % 2 == 1
789 % 10 = 9

It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:

-1 % 2 == -1

In e.g. C++ this is implementation defined.

See also

References

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
6

That is the Modulo operator. It will give you the remainder of a division operation.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
3

It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
2

It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
mikej
  • 65,295
  • 17
  • 152
  • 131
1

That is the modulo operator, which finds the remainder of division of one number by another.

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

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
1

It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.

e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
0

It is modulus 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
šljaker
  • 7,294
  • 14
  • 46
  • 80
0

is basic operator available in almost every language and generally known as modulo operator. it gives remainder as result.

Kinjal
  • 21
  • 2
  • 6
0

Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]


Santhosh
  • 729
  • 7
  • 19
-3

Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.

Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).

Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different. Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)

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();
  }
}

This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.

Blue64
  • 1
  • 3
  • I even utilized Floats on the Exact values so you could clearly see that it explicitly reads `0.000` on the `/` whilst `%` clearly reads `1.000`. I also realized once I posted this `(37%6)=a` by the end, they both `=1` – Blue64 Jun 01 '17 at 13:30
  • 1
    If the Code has invalid Operand, I'd love to see the Suggestion for the correct Code **before** you downvote it. – Blue64 Jun 01 '17 at 13:40