# C# Arithmetic Operator

In C#, arithmetic operators are used to perform mathematical operations on numeric data types, such as `int`

, `double`

, and `decimal`

. The basic arithmetic operators in C# are:

`+`

(addition)`-`

(subtraction)`*`

(multiplication)`/`

(division)`%`

(modulus or remainder)

Here are some examples of how to use arithmetic operators in C#:

int x = 10; int y = 5; int sum = x + y; // sum is 15 int difference = x - y; // difference is 5 int product = x * y; // product is 50 int quotient = x / y; // quotient is 2 int remainder = x % y; // remainder is 0

Note that the modulus operator `%`

returns the remainder of the division operation. In the example above, the remainder of `10 / 5`

is `0`

.

C# also provides shorthand notation for performing arithmetic operations with assignment:

int x = 10; int y = 5; x += y; // equivalent to x = x + y; (x is now 15) x -= y; // equivalent to x = x - y; (x is now 10 again) x *= y; // equivalent to x = x * y; (x is now 50) x /= y; // equivalent to x = x / y; (x is now 10 again) x %= y; // equivalent to x = x % y; (x is now 0)

In addition to the basic arithmetic operators, C# also provides some additional arithmetic operators for working with numeric types, including:

`++`

(increment)`--`

(decrement)`+`

(unary plus)`-`

(unary minus)

The increment and decrement operators can be used to increase or decrease the value of a variable by one. The unary plus and unary minus operators can be used to indicate the sign of a numeric value.

int x = 10; x++; // equivalent to x = x + 1; (x is now 11) x--; // equivalent to x = x - 1; (x is now 10 again) int y = +x; // y is 10 (unary plus) int z = -x; // z is -10 (unary minus)

Keep in mind that when performing arithmetic operations with different types, C# uses type coercion rules to convert one type to another before performing the operation. This can sometimes result in unexpected results or loss of precision. It's important to understand the behavior of type coercion in C# and to ensure that you are working with the correct data types to avoid errors.