



Math is the basis of any computer program. Fortunately, it's also pretty easy to pick up. Here's a basic example of adding two numbers together: int answer = 5 + 10; We can also add numeric variables together, as if they were literal numbers: int num1 = 15; int num2 = 10; int addAnswer = num1 + num2; The other common math operations work the same way, except we use '*' for multiplication, and '/' for division: int subAnswer = num1  num2; int mulAnswer = num1 * num2; int divAnswer = num1 / num2; Beware of division, since trying to divide a number by zero will result in a program crash. Of course, integers cannot store decimals, so any remainder will simply be dropped from the answer. If you want the full decimal answer, you'll have to use a numeric type that supports decimals, like double. What if you want to store the remainder itself? Well, to find a remainder, you can do this: int modAnswer = num1 % num2; The '%' operator is called the "modulus" operator. It finds the remainder of division between two numbers. For example, 11 % 5 would yield an answer of 1 (because five goes into eleven twice, with a remainder of 1). Note that you are not limited to one operation at a time, either. The following code would work just fine: int bigAnswer = (num1  num2) * num2 + num1; Just remember that numbers in brackets are evaluated first, then multiplication and division, and finally addition and subtraction. This precedence of operators in an equation is a universal standard in mathematics, so I won't go any deeper into it here. The .NET framework has a library that contains many common mathematical functions, known simply as the "Math" library. For example, let's say that we had two numbers, and wanted to know which one was bigger. We'd simply do this... int biggestNumber = Math.Max(num1, num2); ...and "biggestNumber" will now store the value of either num1 or num2, whichever was larger. Finding the smallest number works exactly the same way: int smallestNumber = Math.Min(num1, num2); How about rounding? It's nice that doubles can store so many decimal points, but you often don't want a number like 42.4351433462 cluttering up your console output. Here's how you'd round it down: double num3 = 94.537; double twoDecimalPlaces = Math.Round(num3, 2)); This command will round the value of num3 to two decimal places, producing 94.54. The first value we gave it, "num3", is the number that we're rounding, and the second value we gave it, "2", specifies how many decimal places we want the final result to have. So if we wanted to round to the nearest whole number (no decimal places), we'd simply supply a zero: double noDecimalPlaces = Math.Round(num3, 0)); And of course, the result of rounding 94.537 to the nearest whole number would be 95. Of course, for programming purposes, we prefer to use string formatting on a value to change its appearance on the console, rather than playing with the actual value (which we might want to use later). By the way, if you ever need the value of PI in one of your equations, there's a shortcut for that, too. Here it is: double valueOfPI = Math.PI; Of course, since PI is always the same, this is one case where you really don't need (or want) it in a variable. It's best to just use Math.PI as you need it, like so: double circumference = 50 * Math.PI; Calculating powers is easy, too. The following code calculates some powers of two, and immediately writes them to the console (without storing them in their own variables): Console.WriteLine("2 to the power of 2 = " + Math.Pow(2, 2)); Console.WriteLine("2 to the power of 3 = " + Math.Pow(2, 3)); Console.WriteLine("2 to the power of 4 = " + Math.Pow(2, 4)); One last thing, and it's important! Observe the following line of code: double badAnswer = 12 / 5; You might think that, since you're storing the result of the division in a "double", which supports decimal places, "badAnswer" will include the decimal remainder in your answer. In actual fact, it will not. Because you're dividing two integers, 12 and 5, C# assumes that you want to perform integer division, regardless of the datatype you store the answer in. As a result, the decimal part of your answer will be dropped! However, you can force C# to consider the numbers as doubles, by doing this: double goodAnswer = 12.0 / 5.0; Even though this doesn't change the actual value of the numbers, it does change the way that C# sees them. Because you've added a decimal point, C# now assumes that you're dealing with the "double" datatype. What if you did this? goodAnswer = 12 / 5.0; Although one of the numbers you're dividing is still an integer, C# will still treat the entire operation as double division, and not integer division. If your equation contains several numeric datatypes, C# automatically assumes that all the values in your equation are of the most precise type you've used. In this case, the most precise type is a double, so C# treats both numbers as doubles. What about variables? Well, the following code will yield the same bad result you got earlier: int numX = 12; int numY = 5; badAnswer = numX / numY; Because both variables are integers, you'll get an "integer division" result, and therefore you lose the remainder. To prevent this, "cast" at least one of the variables to a double. This forces C# to consider numX as a double in the equation: goodAnswer = (double)numX / numY; Some of the most baffling math errors you can encounter result from your program performing integer division without your realizing it. Always be aware of what datatypes are in play when performing division! Anyways, that's it for the math lesson! Here's a code sample that demonstrates everything you've learned in this part: using System; namespace D_Math { class Class1 { [STAThread] static void Main(string[] args) { // Declare two numbers int num1 = 15; int num2 = 10; // Perform some math on them (store answers) int addAnswer = num1 + num2; int subAnswer = num1  num2; int mulAnswer = num1 * num2; int divAnswer = num1 / num2; int modAnswer = num1 % num2; int bigAnswer = (num1  num2) * num2 + num1; // Display the results Console.WriteLine("Where number1 is {0}, and number2 is {1}...", num1, num2); Console.WriteLine("Addition result: " + addAnswer); Console.WriteLine("Subtraction result: " + subAnswer); Console.WriteLine("Multiplication result: " + mulAnswer); Console.WriteLine("Division result: " + divAnswer); Console.WriteLine("Modulus result: " + modAnswer); Console.WriteLine("BEDMAS result: " + bigAnswer); // Basic number comparison (don't bother storing answers) Console.WriteLine("Bigger Number: " + Math.Max(num1, num2)); Console.WriteLine("Smaller Number: " + Math.Min(num1, num2)); // Rounding double num3 = 94.537; Console.WriteLine(Environment.NewLine + "Original Number:\t" + num3); Console.WriteLine("Rounded to 2 Places:\t" + Math.Round(num3, 2)); Console.WriteLine("Rounded to 1 Places:\t" + Math.Round(num3, 1)); Console.WriteLine("Rounded to 0 Places:\t" + Math.Round(num3, 0)); // PI  it's never been so easy! Console.WriteLine(Environment.NewLine + "PI = " + Math.PI); // Powers Console.WriteLine(Environment.NewLine + "2 to the power of 1 = " + Math.Pow(2, 1)); Console.WriteLine("2 to the power of 2 = " + Math.Pow(2, 2)); Console.WriteLine("2 to the power of 3 = " + Math.Pow(2, 3)); Console.WriteLine("2 to the power of 4 = " + Math.Pow(2, 4)); // (Really) basic calculator! Console.Write(Environment.NewLine + "Enter the 1st number to multiply: "); double mul1 = Convert.ToInt32( Console.ReadLine() ); Console.Write("Enter the 2nd number to multiply: "); double mul2 = Convert.ToInt32( Console.ReadLine() ); Console.WriteLine("{0} * {1} = {2}", mul1, mul2, (mul1 * mul2)); // Beware of "behindthescenes" casting! Console.WriteLine(Environment.NewLine + "12 / 5 = ?"); double badAnswer = 12 / 5; double goodAnswer = 12.0 / 5.0; Console.WriteLine("Bad answer: {0}\tGood Answer: {1}", badAnswer, goodAnswer); // Prompt for exit Console.Write(Environment.NewLine + "Program complete! Hit enter to exit..."); Console.ReadLine(); } } } 

