Home » C# Arithmetic Operators : A Tutorial

C# Arithmetic Operators : A Tutorial

by wskandot71

Arithmetic operators in C# are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and modulus operations. These operators work with numeric data types such as int, double, float, decimal, and long.

This tutorial will cover:

  • List of Arithmetic Operators in C#
  • Using Arithmetic Operators with Examples
  • Operator Precedence and Associativity
  • Handling Division and Modulus Operations
  • Type Conversion in Arithmetic Operations
  • Increment and Decrement Operators
  • Best Practices

1. List of Arithmetic Operators in C#

C# provides the following arithmetic operators:

Operator Description Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b
++ Increment a++ or ++a
Decrement a– or –a

2. Using Arithmetic Operators with Examples

Arithmetic operators work on numeric values and return a result based on the operation.

Addition (+)

int a = 10;
int b = 5;
int sum = a + b;
Console.WriteLine(sum); // Output: 15

Subtraction (-)

int a = 10;
int b = 3;
int difference = a - b;
Console.WriteLine(difference); // Output: 7

Multiplication (*)

int a = 4;
int b = 5;
int product = a * b;
Console.WriteLine(product); // Output: 20

Division (/)

int a = 10;
int b = 2;
int quotient = a / b;
Console.WriteLine(quotient); // Output: 5

Modulus (%)

int a = 10;
int b = 3;
int remainder = a % b;
Console.WriteLine(remainder); // Output: 1
  • The modulus operator (%) returns the remainder of a division.

3. Operator Precedence and Associativity

C# follows the BODMAS (PEMDAS) rule for arithmetic operations.

Precedence Order

Operator Description Associativity
*, /, % Multiplication, Division, Modulus Left to Right
+, – Addition, Subtraction Left to Right

Example of Operator Precedence

int result = 10 + 5 * 2;
Console.WriteLine(result); // Output: 20 (Multiplication is done first)

To change the order of execution, use parentheses:

int result = (10 + 5) * 2;
Console.WriteLine(result); // Output: 30 (Addition is done first)

4. Handling Division and Modulus Operations

Integer Division

When dividing two integers, the result is also an integer.

int a = 7;
int b = 2;
int result = a / b;
Console.WriteLine(result); // Output: 3 (Fractional part is discarded)

Floating-Point Division

To retain decimal values, at least one operand should be double or float.

double x = 7;
double y = 2;
double result = x / y;
Console.WriteLine(result); // Output: 3.5

Avoiding Division by Zero

int a = 10;
int b = 0;
if (b != 0)
{
    Console.WriteLine(a / b);
}
else
{
    Console.WriteLine("Cannot divide by zero");
}

5. Type Conversion in Arithmetic Operations

When performing operations on mixed data types, implicit and explicit conversions occur.

Implicit Conversion

C# automatically converts smaller data types to larger ones.

int a = 10;
double b = 2.5;
double result = a + b; // Implicit conversion of 'a' to double
Console.WriteLine(result); // Output: 12.5

Explicit Conversion (Casting)

To convert larger data types to smaller ones, explicit casting is required.

double pi = 3.14159;
int intValue = (int)pi; // Explicit conversion (fractional part removed)
Console.WriteLine(intValue); // Output: 3

6. Increment (++) and Decrement (–) Operators

The increment and decrement operators modify a variable’s value by 1.

Postfix Increment (a++)

int a = 5;
int b = a++;
Console.WriteLine(a); // Output: 6
Console.WriteLine(b); // Output: 5 (value assigned before increment)

Prefix Increment (++a)

int a = 5;
int b = ++a;
Console.WriteLine(a); // Output: 6
Console.WriteLine(b); // Output: 6 (value assigned after increment)

Postfix Decrement (a–)

int a = 5;
int b = a--;
Console.WriteLine(a); // Output: 4
Console.WriteLine(b); // Output: 5

Prefix Decrement (–a)

int a = 5;
int b = --a;
Console.WriteLine(a); // Output: 4
Console.WriteLine(b); // Output: 4

7. Best Practices for Using Arithmetic Operators

Use floating-point types for precise calculations

double price = 10.99;
double tax = price * 0.18; // Better for precision

Use integer types for counting

int count = 0;
count++;

Handle division carefully to avoid divide-by-zero errors

if (b != 0)
{
    Console.WriteLine(a / b);
}
else
{
    Console.WriteLine("Cannot divide by zero");
}

Use parentheses for clarity

int result = (a + b) * c; // Clear and avoids ambiguity

Use decimal for financial calculations

decimal salary = 4500.75m;
decimal tax = salary * 0.15m;

Conclusion

  • Arithmetic operators perform basic mathematical operations in C#.
  • Operator precedence follows multiplication and division before addition and subtraction.
  • Integer division truncates the decimal part, while floating-point division retains it.
  • Implicit conversions occur automatically, while explicit conversions require casting.
  • Increment (++) and decrement (–) operators modify values by 1.
  • Use best practices to write clear and efficient mathematical expressions.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.