All lessons

Numbers & Math

7 min · C#

Do math with int variables and the basic operators.

C# · Numbers & Math0/5 cleared

Enemy 1 of 5

Arithmetic in C#

The standard operators + - * / work exactly as you'd expect for arithmetic, and follow the usual order of operations you learned in math class (multiplication and division happen before addition and subtraction unless parentheses say otherwise).

As with most statically typed languages, dividing two ints in C# gives you an int result — the decimal portion is truncated, not rounded. 7 / 2 evaluates to 3, not 3.5. If you need a fractional answer, at least one of the values involved needs to be a double instead of an int.

You can pass an expression like price * quantity directly into Console.WriteLine — it doesn't need to be stored in a variable first. WriteLine will evaluate the expression and print the resulting value.

You
Syntax Skeleton

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: 15