# Mathematical Operators

C# Math Operators has available operators for use in mathematical calculations. By combining operators and operands, we have an expression which is how C# makes calculations. Here is a table of available C# Math Operators.

**C# Math Operators**

Operator | Category | Example Expression | Result |
---|---|---|---|

+ | Binary | var1 = var2 + var3; | var1 is assigned with the sum of var2 and var3. |

– | Binary | var1 = var2 – var3; | var1 is assigned with the difference of var2 and var3. |

* | Binary | var1 = var2 * var3; | var1 is assigned with the product of var2 and var3. |

/ | Binary | var1 = var2 / var3; | var1 is assigned with the quotient of var2 and var3. |

% | Binary | var1 = var2 % var3; | var1 is assigned with the remainder resulted from the division of var2 and var 3. |

+ | Unary | var1 = +var2; | var1 is assigned with the value of var2 |

– | Unary | var1 = -var2 | var1 is assigned with the value of var2 multiplied by -1. |

The example above uses numeric types. But using this on other types such as string can have different results. Likewise, adding two charwill result on the compiler converting their value to their respective numerical equivalents. When the + operator is used with string data, it will concatenate the two strings and combine them into one.

Another set of operators in C# are the increment and decrement operators. These operators increases or decreases the value of a variable by 1. This operators are often used for looping.

Operator | Category | Example Expression | Result |
---|---|---|---|

++ | Unary | var1 = ++var2; | var1 is assigned the value of var2 + 1. var2 is incremented by 1. |

— | Unary | var1 = –var2; | var1 is assgine the value of var2 – 1. var2 is decremented by 1. |

++ | Unary | var1 = var2++; | var1 is assigned the value of var2. var2 is incremented by 1. |

— | Unary | var1 = var2–; | var1 is assigned the value of var2.. var2 is decrementday by 1. |

Note that the position of the two operators determines the effect of the calculation. If the operator is a prefix to the variable, the increment or decrement operation will took place first before being assigned to var1. Contrast to when you place it at the end of var2, the value assigned to var1 is the value of var2, the increment or decrement for var2 will happen after it’s value is assigned to var1.

We will now try what we learned by creating a new program that tests the mathematical operators of C#.

```
using System;
public class <span class="csType">Program</span>
{
public static void Main()
{
//Variable declarations
int num1, num2;
string msg1, msg2;
//Assign test values
num1 = 5;
num2 = 3;
//Demonstrate use of mathematical operators
Console.WriteLine("The sum of {0} and {1} is {2}.", num1, num2, (num1 + num2));
Console.WriteLine("The difference of {0} and {1} is {2}.", num1, num2,
(num1 - num2));
Console.WriteLine("The product of {0} and {1} is {2}.", num1, num2,
(num1 * num2));
Console.WriteLine("The quotient of {0} and {1} is {2:F2}.", num1, num2,
((double)num1 / num2));
Console.WriteLine("The remainder of {0} divided by {1} is {2}", num1, num2,
(num1 % num2));
//Demonstrate concatenation on strings using the + operator
msg1 = "Hello ";
msg2 = "World!";
Console.WriteLine(msg1 + msg2);
}
}
```

Example 1

The sum of 5 and 3 is 8. The difference of 5 and 3 is 2. The product of 5 and 3 is 15. The quotient of 5 and 3 is 1.67. The remainder of 5 divided by 3 is 2 Hello World!

The program demonstrates the expected results for each expressions. Again, we use the overloaded version of the Console.Writeline() to make it easier to write and format. There is one odd thing you might notice when dividing the two integers. If we divide two integers, it will yield another integer. That would drop out all the fractional part of the result and give us only a whole number. In line 22, we convert at least one of the operand to double to get the right result. Then we format the result to only show 2 decimal places using {2:F2}. F means fixed and 2 means the number of decimal places to include. Note that the number is rounded. The statement is too long so we split it into two lines. C# ignores newlines, tabs and white spaces so there is no problem about this.

Line 29 shows string concatenation using the + operator on strings. When we used the + operator for the two string data, we get a different result. Adding “Hello “ and “World!” gives us “Hello World!”. Take note of the extra space added to the end of the first word. Omitting this will also omit the space between the two words in the final output.