What does the '%' operator mean?

后端 未结 11 1716
执笔经年
执笔经年 2020-12-11 18:28

I have next code

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

What does \"%\" operator mean?

相关标签:
11条回答
  • 2020-12-11 18:39

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

    0 讨论(0)
  • 2020-12-11 18:41

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

    0 讨论(0)
  • 2020-12-11 18:43

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

    0 讨论(0)
  • 2020-12-11 18:49

    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.

    0 讨论(0)
  • 2020-12-11 18:50

    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.

    0 讨论(0)
  • 2020-12-11 18:56

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

    0 讨论(0)
提交回复
热议问题