Start learning free

LESSON 15 OF 347 · CALCULATIONS WITH NUMBERS

Arithmetic Operators in C#: +, -, *, / and %

Lesson Read the full explanation

TL ; DR:

  • We can tell computers to perform calculations for us using +, -, *, /:

    Console.WriteLine(2 + 6); // Prints 8
    Console.WriteLine(10 - 7); // Prints 3
    Console.WriteLine(3 * 3); // Prints 9
    Console.WriteLine(6 / 2); // Prints 3
    


  • We can also use variables. In these examples, C# will replace variable num with its value 6 to do the calculations:

    int num = 6;
    Console.WriteLine(num + 5); // Prints 11
    Console.WriteLine(10 - num); // Prints 4
    Console.WriteLine(3 * num); // Prints 18
    Console.WriteLine(num / 2); // Prints 3
    



Full lesson:

Computers absolutely excel at performing calculations. The "compute" in their name comes from their historical association with providing answers to mathematical questions.

These calculations can be performed either directly with numbers or with variables that are assigned numeric values.

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Examples
+ Addition 3 + 5,  x + 5,  3 + y,  x + y
- Subtraction 8 - 3,  8 - y,  x - 3,  x - y
* Multiplication 3 * 5,  3 * y,  x * 5,  x * y
/ Division 8 / 4,  8 / y,  x / 8,  x / y
% Modulus 8 % 4,   8 % y,  x % 4,  x % y


Note that performing arithmetic on variables does not change the variable - you can only update a variable using the = sign.

Let's go through some examples together to better understand how each operator works:


Addition

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

Console.WriteLine(a + b); // Output: 5

Console.WriteLine(a + 10); // Output: 12

Subtraction

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

Console.WriteLine(a - b); // Output: -1

Console.WriteLine(10 - b); // Output: 7

Multiplication

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

Console.WriteLine(a * 5); // Output: 10

Console.WriteLine(2 * a * b); // Output: 12

Division

In Java, division between integers returns only the integral part of the result:

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

Console.WriteLine(20 / 3); // Output: 6

Console.WriteLine(b / a); // Output: 1

If we want to get the real result of the division, at least one of the terms has to be of type "double":

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

Console.WriteLine((double) 7 / 3); // Output: 2.5

Console.WriteLine(b / (double) a); // Output: 1.5

Assignment
Follow the Coding Tutorial and let's practice with arithmetic operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Arithmetic Operators in C#, explained

    The five operators

    C# can calculate with numbers directly, or with variables that hold numbers. The arithmetic operators are:

    OperatorNameExampleResult
    +Addition2 + 68
    -Subtraction10 - 73
    *Multiplication3 * 39
    /Division6 / 23
    %Remainder (modulus)8 % 32

    When a variable appears in an expression, C# uses its current value:

    int num = 6;
    Console.WriteLine(num + 5);    // 11
    Console.WriteLine(10 - num);   // 4
    Console.WriteLine(3 * num);    // 18
    Console.WriteLine(num / 2);    // 3
    

    An expression only calculates a value, and that value is used wherever the expression appears. It does not change the variables in it. After the lines above, num is still 6. To change a variable you must assign the result with =, for example num = num + 5;.

    Integer division

    This is the detail that surprises beginners. When both operands are integers, / performs integer division and throws away the fractional part. It truncates toward zero and does not round:

    Console.WriteLine(20 / 3);   // 6
    Console.WriteLine(3 / 2);    // 1
    

    To get a fractional result, at least one operand must be a double. A double literal has a decimal point:

    Console.WriteLine(7 / 2.0);   // 3.5
    

    Casting is the other route, but mind where the cast applies. A cast binds tighter than /, so (double) 7 / 3 converts only the 7 and gives 2.3333333333333335. Casting either operand is enough: 3 / (double) 2 gives 1.5. And (double) (7 / 3) is wrong in another way, since the integer division happens first, leaving 2. Order matters in longer expressions too: 7 / 2 * 2.0 is 6, because 7 / 2 runs first as integer division, while 7 / 2.0 * 2 is 7.

    The rule is that C# converts the int to a double whenever the two operands differ, and the result is a double. That result can't be stored in an int variable without a cast, so int half = 7 / 2.0; fails to compile. Use double half = 7 / 2.0; instead.

    The remainder operator

    % gives what is left over after integer division. 8 % 3 is 2, because 3 fits into 8 twice with 2 left. It is handy for checking whether a number is even (n % 2 == 0) or for wrapping around a range. With negative numbers, the result takes the sign of the left operand: -7 % 3 is -1.

    Order of evaluation

    C# follows the usual rules: multiplication, division and remainder happen before addition and subtraction, and parentheses override both.

    Console.WriteLine(2 + 3 * 4);     // 14
    Console.WriteLine((2 + 3) * 4);   // 20
    

    Shortcuts for updating a variable

    Assigning the result of an operation back to the same variable is so common that C# has shorthand forms:

    int score = 10;
    score += 5;   // same as score = score + 5;  now 15
    score *= 2;   // same as score = score * 2;  now 30
    score++;      // adds one; now 31
    

    These are assignments, so they do change the variable.

    Things that go wrong

    • Overflow. An int has a fixed range, up to 2,147,483,647. In ordinary code, going past it does not fail: the value wraps around, so if big holds int.MaxValue, big + 1 is -2147483648. (Written with constants, int.MaxValue + 1 is caught by the compiler instead.)
    • Dividing integers by zero. a / 0 with an int variable throws a DivideByZeroException at run time. With double, the result is Infinity instead of an exception.
    • Floating-point noise. 0.1 + 0.2 prints 0.30000000000000004 because double stores binary approximations.
    • Mixing text and numbers. "Total: " + 1 + 2 joins text and gives Total: 12; put the sum in parentheses.

    The exercise, step by step

    The variable price holds the cost of one product, 83. Andy buys 4 products at that price and one bag for 15, and the program must print the total.

    1. Multiply the quantity by the price. 4 * price is the cost of the products.
    2. Add the bag. Adding 15 gives 4 * price + 15.
    3. Put it inside Console.WriteLine. Parentheses aren't needed, since multiplication runs before addition. Writing (4 * price) + 15 would be harmless but is redundant.
    int price = 83;
    Console.WriteLine(4 * price + 15);
    

    The program prints 347: 4 times 83 is 332, and 332 plus 15 is 347. If the addition ran first, you would get 4 * (83 + 15), which is 392, so precedence changes the answer.

    Key takeaways

    • +, -, *, / and % work on numbers and variables holding numbers.
    • Integer division discards the fraction; make one operand a double to keep it.
    • Multiplication, division and remainder come before addition and subtraction.
    • Expressions don't change variables; assignment does.
    • Mixing an int and a double gives a double, and storing that in an int needs an explicit cast.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free