Arithmetic operators are used frequently in order to calculate and process data in your program. You will use them a lot for different purposes and are important to understand.
Let's briefly look at what all operators does and what they look like
+ adds numbers:
- subtracts numbers from each other:
* multiplies values:
/ divides numbers:
% is called the "modulus operator" and it also performs division. However, it only returns the remainder of what's left:
When it comes to arithmetic, it works exactly like it does in maths. We follow the basic rules of mathematics for calculating.
Tip: When calculating, it is often a good habit to use paranthesis, even if the program works without them, to avoid making some basic math mistakes. Sometimes you'll want to type
(10 + 3 + 5 + 7) instead of just
10 + 3 + 5 + 7 even if it seems obvious to you that they're the same!
Create a program that calculates the avarage value of a bunch of numbers. If your values are , and , the result should be .
Remember the arithmetic order for how the calculation works. Paranthesis, multiply/division and addition/subtraction.
Declare a few variables and set them to a value of your choice
Store the calculation in another variable
Print out the result!