C# uses five mathematical operators: four for standard calculations and one to return the remainder when dividing integers. The following sections consider the use of these operators.
C# offers four operators for simple arithmetic: the addition (+
), subtraction (–
), multiplication (*
), and division (/
) operators. The +
and –
operators are obvious, and work as you might expect. The *
operator for multiplication may look a bit odd if you’re not used to it, but there’s nothing else special about it. Division, however, is slightly unusual, depending on the types you’re dividing.
When you divide two integers, C# divides like a child in the third grade: it throws away any fractional remainder. Thus, dividing 17 by 4 returns a value of 4 (C# discards the remainder of 1).
This limitation is specific to integer division. If you do not want the fractional part thrown away, you can use one of the types that support decimal values, such as float
or double
. Division between two floats (using the /
operator) returns a decimal answer. Integer and floating-point division is illustrated in Example 4-1.
Example 4-1. Integer division is different from float division; in integer division, C# discards the remainder
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example_4_1_ _ _ _Integer_and_Float_Division { class Program { public static void Main( ) { int smallInt = 5; int largeInt = 12; int intQuotient; intQuotient = largeInt / smallInt; Console.WriteLine("Dividing integers. {0} / {1} = {2}", largeInt, smallInt, intQuotient); float smallFloat = 5; float largeFloat = 12; float FloatQuotient; FloatQuotient = largeFloat / smallFloat; Console.WriteLine("Dividing floats. {0} / {1} = {2}", largeFloat, smallFloat, FloatQuotient); } } }
The output looks like this:
Dividing integers. 12 / 5 = 2 Dividing floats. 12 / 5 = 2.4
Of course, you might want to calculate the remainder from an integer division, not throw it away. For that, C# provides a special operator, modulus (%
), to retrieve the remainder. For example, the statement 17%4
returns 1
(the remainder after integer division).
Tip
You read that statement as “Seventeen modulo four equals one” or, for short, “Seventeen mod four.”
Example 4-2 demonstrates the effect of division on integers, floats, doubles, and decimals. Notice the escaped characters used in the output, which we discussed in “WriteLine( ) and Output” in Chapter 3.
Example 4-2. The modulus operator (%) is what you use to get the remainder from an integer division operation
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example_4_2_ _ _ _Modulus_operator { class ValuesProgram { static void Main( ) { int firstInt, secondInt; float firstFloat, secondFloat; double firstDouble, secondDouble; decimal firstDecimal, secondDecimal; firstInt = 17; secondInt = 4; firstFloat = 17; secondFloat = 4; firstDouble = 17; secondDouble = 4; firstDecimal = 17; secondDecimal = 4; Console.WriteLine("Integer:\t{0}\nfloat:\t\t{1}", firstInt / secondInt, firstFloat / secondFloat); Console.WriteLine("double:\t\t{0}\ndecimal:\t{1}", firstDouble / secondDouble, firstDecimal / secondDecimal); Console.WriteLine("\nRemainder (modulus) from integer division:\t{0}", firstInt % secondInt); } } }
Integer: 4 float: 4.25 double: 4.25 decimal: 4.25 Remainder(modulus) from integer division: 1
Tip
The modulus operator is more than a curiosity; it greatly simplifies finding every nth value, as you’ll see in Chapter 5.
Get Learning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.