Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the '%' operator mean?

Tags:

operators

c#

I have next code

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

What does "%" operator mean?

like image 751
Polaris Avatar asked Jul 16 '10 11:07

Polaris


People also ask

What does '%' mean in programming?

The % operator is one of the "Arithmetic Operators" in JavaScript, like / , * , + , and - . The % operator returns the remainder of two numbers. It is useful for detecting even/odd numbers (like to make stripes) and for restricting a value to a range (like to wrapping an animated ball around) .

What does the operator <> indicate?

An operator, in computer programing, is a symbol that usually represents an action or process. These symbols were adapted from mathematics and logic. An operator is capable of manipulating a certain value or operand.

What does the operator mean in C++?

In programming, an operator is a symbol that operates on a value or a variable. Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction. Operators in C++ can be classified into 6 types: Arithmetic Operators.


Video Answer


1 Answers

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)

like image 80
Dirk Vollmar Avatar answered Jan 05 '23 13:01

Dirk Vollmar