Maths operators deal with numbers only. As such both variables or literals must be numbers. The following example should cause an error -
A = 4 * "hello" |
The following operators are available in most programming languages -
Operator |
What it does |
Example |
+ | Addition | A = 5 + 4 (Result = 9) |
- | Subtraction | A = 5 - 4 (Result = 1) |
* | Multiplication | A = 5 * 4 (Result = 20) |
/ | Division | A = 8 / 4 (Result = 2) |
MOD | Modulus | A = 12 MOD 10 (Result = 2) |
DIV | Rounded division | A = 12 DIV 10 (Result = 1) |
MOD operator stands for modulus (Sometimes written as % in some languages). This divides numbers but instead of giving the answer it will return the remainder. For example
44 MOD 10
44 divided by 10 is 4 remainder 4. As such the above bit of code will return the value 4.
DIV operator will basically return the rounded answer.
44 DIV 10
The above code will return 4 as the answer. Using DIV and MOD together you can get the full answer without decimal points. This is very useful in programming and a number of tricks can be done with these operators. One classic example is barcodes and check digits.
NOTE - Java will use the % symbol for MOD. DIV is done though rounding or casting to a int.