Introduction
Arithmetic operators are used to perform mathematical operations on variables and values. C# provides a set of arithmetic operators that you can use to perform common mathematical calculations such as addition, subtraction, multiplication, division, and more. Understanding how to use these operators is essential for performing calculations and manipulating data in your programs.
List of Arithmetic Operators
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Increment (++)
- Decrement (–)
1. Addition (+)
The addition operator adds two operands.
Syntax
result = operand1 + operand2;
Example
int a = 5;
int b = 3;
int sum = a + b;
Console.WriteLine("Sum: " + sum);
Output:
Sum: 8
2. Subtraction (-)
The subtraction operator subtracts the second operand from the first.
Syntax
result = operand1 - operand2;
Example
int a = 5;
int b = 3;
int difference = a - b;
Console.WriteLine("Difference: " + difference);
Output:
Difference: 2
3. Multiplication (*)
The multiplication operator multiplies two operands.
Syntax
result = operand1 * operand2;
Example
int a = 5;
int b = 3;
int product = a * b;
Console.WriteLine("Product: " + product);
Output:
Product: 15
4. Division (/)
The division operator divides the first operand by the second. If both operands are integers, the result is an integer (the quotient).
Syntax
result = operand1 / operand2;
Example
int a = 10;
int b = 2;
int quotient = a / b;
Console.WriteLine("Quotient: " + quotient);
Output:
Quotient: 5
Example with Floating-Point Division
double x = 5.0;
double y = 2.0;
double quotient = x / y;
Console.WriteLine("Quotient: " + quotient);
Output:
Quotient: 2.5
5. Modulus (%)
The modulus operator returns the remainder when the first operand is divided by the second.
Syntax
result = operand1 % operand2;
Example
int a = 10;
int b = 3;
int remainder = a % b;
Console.WriteLine("Remainder: " + remainder);
Output:
Remainder: 1
6. Increment (++)
The increment operator increases the value of a variable by 1. It can be used in both prefix and postfix forms.
Syntax
++variable; // Prefix
variable++; // Postfix
Example
int a = 5;
a++;
Console.WriteLine("Incremented Value (Postfix): " + a);
Output:
Incremented Value (Postfix): 6
Example with Prefix
int a = 5;
++a;
Console.WriteLine("Incremented Value (Prefix): " + a);
Output:
Incremented Value (Prefix): 6
7. Decrement (–)
The decrement operator decreases the value of a variable by 1. It can be used in both prefix and postfix forms.
Syntax
--variable; // Prefix
variable--; // Postfix
Example
int a = 5;
a--;
Console.WriteLine("Decremented Value (Postfix): " + a);
Output:
Decremented Value (Postfix): 4
Example with Prefix
int a = 5;
--a;
Console.WriteLine("Decremented Value (Prefix): " + a);
Output:
Decremented Value (Prefix): 4
Combining Arithmetic Operators
You can combine multiple arithmetic operations in a single expression. Parentheses can be used to control the order of operations.
Example
int a = 10;
int b = 5;
int c = 2;
int result = (a + b) * c;
Console.WriteLine("Result: " + result);
Output:
Result: 30
Example with Complex Expression
int a = 10;
int b = 5;
int c = 2;
int d = 3;
int result = (a + b - c) * d;
Console.WriteLine("Complex Result: " + result);
Output:
Complex Result: 39
Conclusion
Understanding and using arithmetic operators is essential for performing mathematical calculations in your C# programs. These operators allow you to add, subtract, multiply, divide, and find the remainder of numbers, as well as increment and decrement values. By mastering these operators, you can create more complex and dynamic calculations in your code.