|
The modulus operator saves the programmer the time for developing their own remainder operator. The remainder operator simply returns the remainder of a division operation. For example:
C# Modulus (remainder) Operator
|
Operator |
Name |
Description |
|
% |
Modulus(Remainder) |
Returns the remainder of a division operation. |
- 10 / 2 = 5 with remainder of 0 --- 10 % 2 = 0 Remainder: 0
- 12 / 2 = 6 with remainder of 0 --- 12 % 2 = 0 Remainder: 0
- 4 / 4 = 1 with remainder of 0 --- 4 % 4 = 0 Remainder: 0
[edit] C# Modulus (remainder) Example
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:
|