An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
| Operator | Description | Example |
|---|---|---|
| + | Adds two operands | A + B = 30 |
| - | Subtracts second operand from the first | A - B = -10 |
| * | Multiplies both operands | A * B = 200 |
| / | Divides numerator by de-numerator | B / A = 2 |
| % | Modulus Operator and remainder of after an integer division | B % A = 0 |
| ++ | Increment operator increases integer value by one | A++ = 11 |
| -- | Decrement operator decreases integer value by one | A-- = 9 |
Let us see an example to use arithmetic operators in C#.
Example
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int a = 99;
int b = 33;
int c;
c = a + b;
Console.WriteLine("Value of c is {0}", c);
c = a - b;
Console.WriteLine("Value of c is {0}", c);
c = a * b;
Console.WriteLine("Value of c is {0}", c);
c = a / b;
Console.WriteLine("Value of c is {0}", c);
c = a % b;
Console.WriteLine("Value of c is {0}", c);
c = a++;
Console.WriteLine("Value of c is {0}", c);
c = a--;
Console.WriteLine("Value of c is {0}", c);
Console.ReadLine();
}
}
}Output
Value of c is 132 Value of c is 66 Value of c is 3267 Value of c is 3 Value of c is 0 Value of c is 99 Value of c is 100