How to Add, Subtract, Multiply and Divide Numbers in C#
C# adds with +, subtracts with -, multiplies with * and divides with /. Two results surprise beginners: 17 / 5 is 3, not 3.4, and 2 + 3 * 4 is 14, not 20.
The C# reference: The following operators perform arithmetic operations with operands of numeric types: Its list of binary operators: Binary * (multiplication), / (division), % (remainder), + (addition), and - (subtraction) operators. And: All integral and floating-point numeric types support these operators.
This post covers the four you use to add, subtract, multiply and divide.
Syntax
<left> + <right>
<left> - <right>
<left> * <right>
<left> / <right>
| Part | What it is |
|---|---|
<left>, <right> | The numbers: variables, literals such as 5, or other calculations |
+ | Adds the two numbers |
- | Subtracts <right> from <left> |
* | Multiplies the two numbers |
/ | Divides <left> by <right>. With two whole numbers, the result is a whole number too |
Each line is an expression: you write it where a value goes, such as after = or inside Console.WriteLine. When an expression has several operators, the reference lists * and / in a group above + and -, and adds: Binary arithmetic operators are left-associative. That is, the compiler evaluates operators with the same precedence level from left to right.
The Example
A console app called Arithmetic adds, subtracts, multiplies and divides 17 and 5, divides them again as doubles, and works out 2 + 3 * 4 with and without parentheses.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n Arithmetic
cd Arithmetic
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/Arithmetic/Arithmetic.csproj:
Determining projects to restore...
Restored /Users/you/source/Arithmetic/Arithmetic.csproj (in 45 ms).
Restore succeeded.
dotnet new console -n Arithmetic creates a folder named Arithmetic with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or Arithmetic.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the culture and the four operators:
using System.Globalization;
// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
int a = 17;
int b = 5;
Console.WriteLine($"{a} + {b} = {a + b}");
Console.WriteLine($"{a} - {b} = {a - b}");
Console.WriteLine($"{a} * {b} = {a * b}");
Console.WriteLine($"{a} / {b} = {a / b}");
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;makes the program print a full stop before decimals on every computer. With the current culture set to German instead, the division of the doubles below printed3,4.int a = 17;stores a whole number in anintvariable.- Each line prints the calculation as text, then its result:
{a + b}is replaced by the sum.
Then the same division with doubles:
// The same numbers as doubles keep the fraction.
double x = 17;
double y = 5;
Console.WriteLine($"{x} / {y} as doubles = {x / y}");
double is the type for numbers with a fraction, and double x = 17; stores 17 as one.
Then the order of the operators:
// * and / come before + and -. Parentheses change the order.
Console.WriteLine($"2 + 3 * 4 = {2 + 3 * 4}");
Console.WriteLine($"(2 + 3) * 4 = {(2 + 3) * 4}");
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 / 5 as doubles = 3.4
2 + 3 * 4 = 14
(2 + 3) * 4 = 20

+,-and*gave 22, 12 and 85.17 / 5gave 3: both numbers areint, so the fraction was dropped. The reference: For the operands of integer types, the result of the/operator is of an integer type and equals the quotient of the two operands rounded toward zero- As doubles, the same division gave 3.4. The reference: To get the quotient of the two operands as a floating-point number, use the
float,double, ordecimaltype 2 + 3 * 4gave 14: the multiplication ran first, then 2 was added.(2 + 3) * 4gave 20, because the reference says: Use parentheses,(), to change the order of evaluation imposed by operator precedence and associativity.
The Complete File

using System.Globalization;
// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
int a = 17;
int b = 5;
Console.WriteLine($"{a} + {b} = {a + b}");
Console.WriteLine($"{a} - {b} = {a - b}");
Console.WriteLine($"{a} * {b} = {a * b}");
Console.WriteLine($"{a} / {b} = {a / b}");
// The same numbers as doubles keep the fraction.
double x = 17;
double y = 5;
Console.WriteLine($"{x} / {y} as doubles = {x / y}");
// * and / come before + and -. Parentheses change the order.
Console.WriteLine($"2 + 3 * 4 = {2 + 3 * 4}");
Console.WriteLine($"(2 + 3) * 4 = {(2 + 3) * 4}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting a fraction from two int values, as in double half = 7 / 2; | half was 3: the division ran on two int values first, and only its result went into the double | Make one side a double: 7 / 2.0 gave 3.5 |
Forgetting that * and / come first, as in 10 + 20 / 2 for an average | It gave 20: 20 / 2 ran first, then 10 was added | Put parentheses around the part that comes first: (10 + 20) / 2 gave 15 |
Dividing an int by zero | System.DivideByZeroException: Attempted to divide by zero. The reference: Integer division by zero always throws a DivideByZeroException. With a literal zero, as in 17 / 0, it doesn't build: error CS0020: Division by constant zero | Check that the number you divide by isn't 0 before dividing |
FAQ
What happens when a result is too big for an int?
It wraps around without an error: int.MaxValue + 1 gave -2147483648. The reference: By default, arithmetic operations occur in an unchecked context. And: In an unchecked context, the result is truncated by discarding any high-order bits that don't fit in the destination type. Inside checked(...), the same sum threw OverflowException: Arithmetic operation resulted in an overflow., and converted to a long first, (long)int.MaxValue + 1 gave 2147483648.
What does dividing a double by zero give?
Infinity, without an exception: 17 divided by a double holding 0 printed Infinity. The reference: Arithmetic operations by using the float and double types never throw an exception.
Conclusion
+, -, * and / work on every numeric type in C#. Remember that / on two whole numbers drops the fraction, so make one side a double when you need it, and that * and / run before + and - unless parentheses say otherwise.
Additional Resources
- Arithmetic operators — every arithmetic operator, with precedence, overflow and division by zero.
- Operator precedence — the order of all the C# operators.
- Floating-point numeric types — double and the other types for numbers with a fraction.
