Skip to main content

How to Add, Subtract, Multiply and Divide Numbers in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<left> + <right>
<left> - <right>
<left> * <right>
<left> / <right>
PartWhat 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 --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n Arithmetic
cd Arithmetic
Output
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:

Program.cs (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 printed 3,4.
  • int a = 17; stores a whole number in an int variable.
  • Each line prints the calculation as text, then its result: {a + b} is replaced by the sum.

Then the same division with doubles:

Program.cs (dividing 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:

Program.cs (the order)
// * 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:

Terminal
dotnet run
Output
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

macOS-style terminal window titled Terminal — Arithmetic showing dotnet run printing 17 plus 5 = 22, 17 minus 5 = 12, 17 times 5 = 85, 17 divided by 5 = 3, 17 divided by 5 as doubles = 3.4, 2 plus 3 times 4 = 14 and the same with parentheses around 2 plus 3 = 20

  • +, - and * gave 22, 12 and 85.
  • 17 / 5 gave 3: both numbers are int, 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, or decimal type
  • 2 + 3 * 4 gave 14: the multiplication ran first, then 2 was added. (2 + 3) * 4 gave 20, because the reference says: Use parentheses, (), to change the order of evaluation imposed by operator precedence and associativity.

The Complete File​

macOS-style code window titled Program.cs — Arithmetic showing the invariant culture set as the current culture, two int variables 17 and 5 added, subtracted, multiplied and divided, the same numbers divided as doubles, and 2 plus 3 times 4 printed with and without parentheses

Program.cs
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​

MistakeWhat happensDo 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 doubleMake one side a double: 7 / 2.0 gave 3.5
Forgetting that * and / come first, as in 10 + 20 / 2 for an averageIt gave 20: 20 / 2 ran first, then 10 was addedPut parentheses around the part that comes first: (10 + 20) / 2 gave 15
Dividing an int by zeroSystem.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 zeroCheck 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​