Arithmetic Operators in C#

Vaibhav • September 11, 2025

Arithmetic operators are the building blocks of computation in any programming language. In C#, they allow you to perform basic mathematical operations like addition, subtraction, multiplication, division, and remainder calculation. These operators work with numeric types such as int, double, and decimal, which you’ve already encountered in Chapter 2. In this article, we’ll explore how these operators behave, how expressions are evaluated, and what to watch out for when using them.

Understanding the Core Arithmetic Operators

C# provides five primary arithmetic operators:

+   // Addition
-   // Subtraction
*   // Multiplication
/   // Division
%   // Modulus (remainder)

These operators can be used with both integer and floating-point types. Let’s look at some examples to see them in action.

Working with Integers

When you use arithmetic operators with integers, the results are also integers. This means that division will discard any fractional part.

int a = 10;
int b = 3;

int sum = a + b;       // 13
int difference = a - b; // 7
int product = a * b;    // 30
int quotient = a / b;   // 3 (not 3.333...)
int remainder = a % b;  // 1

In the above example:

  • a + b adds 10 and 3 to get 13.
  • a - b subtracts 3 from 10 to get 7.
  • a * b multiplies 10 by 3 to get 30.
  • a / b divides 10 by 3, but since both are integers, the result is truncated to 3.
  • a % b gives the remainder of 10 divided by 3, which is 1.

Integer division always truncates the result. If you need a precise result with decimals, use floating-point types.

Working with Floating-Point Numbers

Floating-point types like double and float preserve the fractional part during division.

double x = 10.0;
double y = 3.0;

double result = x / y;  // 3.333333...
double mod = x % y;     // 1.0

Here, x / y gives a precise result, and x % y still works to provide the remainder, but in floating-point form.

The modulus operator % works with both integers and floating-point numbers in C#. It returns the remainder after division.

Combining Operators in Expressions

You can combine multiple arithmetic operators in a single expression. C# follows standard mathematical precedence rules:

int result = 10 + 2 * 3; // 16
int result2 = (10 + 2) * 3; // 36

In the first line, multiplication happens before addition, so 2 * 3 is 6, and then 10 + 6 gives 16. In the second line, parentheses change the order: 10 + 2 is 12, and then 12 * 3 gives 36.

Use parentheses to make your intent clear. Even if the precedence rules would give the correct result, parentheses improve readability and reduce mistakes.

Negative Numbers and Arithmetic

Arithmetic operators work naturally with negative numbers. Subtraction and multiplication behave as expected.

int a = -5;
int b = 4;

int result = a * b; // -20
int result2 = a - b; // -9

Multiplying a negative number by a positive number gives a negative result. Subtracting a positive number from a negative number moves further into the negative range.

Division and Remainder Nuances

Division and remainder can behave differently depending on the sign of the operands.

int a = -10;
int b = 3;

int quotient = a / b;   // -3
int remainder = a % b;  // -1

In C#, the sign of the remainder follows the sign of the dividend. So -10 % 3 gives -1.

In some languages, the remainder always comes out positive. C# follows the rule that the remainder has the same sign as the dividend.

Arithmetic with Mixed Types

When you mix types (like int and double), C# performs implicit conversion to preserve precision.

int a = 5;
double b = 2.0;

double result = a / b; // 2.5

Here, a is converted to double before division, so the result is a floating-point number.

Mixing types can be useful, but be aware of precision loss when converting from double to int. We’ll explore casting in more detail later.

Common Mistakes to Avoid

  • Integer division surprises: dividing two integers gives an integer result. Use floating-point types if you need decimals.
  • Operator precedence confusion: forgetting parentheses can lead to unexpected results.
  • Modulus with negative numbers: remember that the sign of the remainder follows the dividend.
  • Mixing types without understanding conversions: implicit conversions can change the result type and behavior.

Real-World Analogies

Arithmetic operators are like basic tools in a toolbox. Addition is like combining two piles of apples. Subtraction is taking some apples away. Multiplication is like having multiple identical piles. Division is splitting a pile into equal parts. Modulus is asking, “How many apples are left after making full piles?”

Summary

Arithmetic operators in C# are simple yet powerful. They allow you to perform basic mathematical operations with both integers and floating-point numbers. Understanding how division and remainder behave, especially with different types and signs, is crucial for writing correct programs. Use parentheses to clarify expressions, and be mindful of type conversions when mixing numeric types. These operators form the foundation for more complex logic and calculations you’ll build in future chapters.