Information Technology Reference
In-Depth Information
The Remainder Operator
The remainder operator ( % ) divides the first operand by the second operand, ignores the quo-
tient, and returns the remainder. Its description is given in Table 8-7.
The remainder operator is binary and left-associative.
Table 8-7. The Remainder Operator
Operator
Name
Description
%
Remainder
Divides the first operand by the second operand and returns the
remainder
The following lines show examples of the integer remainder operator :
￿0 % 3 = 0, because 0 divided by 3 is 0 with a remainder of 0.
￿1 % 3 = 1, because 1 divided by 3 is 0 with a remainder of 1.
￿2 % 3 = 2, because 2 divided by 3 is 0 with a remainder of 2.
￿3 % 3 = 0, because 3 divided by 3 is 1 with a remainder of 0.
￿4 % 3 = 1, because 4 divided by 3 is 1 with a remainder of 1.
The remainder operator can also be used with real numbers to give real remainders .
Console.WriteLine("0.0f % 1.5f is {0}", 0.0f % 1.5f);
Console.WriteLine("0.5f % 1.5f is {0}", 0.5f % 1.5f);
Console.WriteLine("1.0f % 1.5f is {0}", 1.0f % 1.5f);
Console.WriteLine("1.5f % 1.5f is {0}", 1.5f % 1.5f);
Console.WriteLine("2.0f % 1.5f is {0}", 2.0f % 1.5f);
Console.WriteLine("2.5f % 1.5f is {0}", 2.5f % 1.5f);
This code produces the following output:
0.0f % 1.5f is 0 // 0.0 / 1.5 = 0 remainder 0
0.5f % 1.5f is 0.5 // 0.5 / 1.5 = 0 remainder .5
1.0f % 1.5f is 1 // 1.0 / 1.5 = 0 remainder 1
1.5f % 1.5f is 0 // 1.5 / 1.5 = 1 remainder 0
2.0f % 1.5f is 0.5 // 2.0 / 1.5 = 1 remainder .5
2.5f % 1.5f is 1 // 2.5 / 1.5 = 1 remainder 1
Search WWH ::




Custom Search