Arithmetic in Programming – Maths in Code
Every computer program runs on arithmetic. From the simplest calculator app to artificial intelligence, the four operations are the foundation of all computation.
Arithmetic Operators in Code
| Operation | Python / JS | Example | Result |
|---|---|---|---|
| Addition | + | 5 + 3 | 8 |
| Subtraction | − | 10 − 4 | 6 |
| Multiplication | * | 6 * 7 | 42 |
| Division | / | 15 / 4 | 3.75 |
| Integer division | // | 15 // 4 | 3 |
| Modulo (remainder) | % | 15 % 4 | 3 |
| Exponentiation | ** | 2 ** 8 | 256 |
Order of Operations in Code
Programming languages follow the same precedence rules as BODMAS. Parentheses override everything.
3 + 4 * 2 → 11 (not 14) because * happens before +
Integer vs Float Division
15 / 4 = 3.75 (float division)
15 // 4 = 3 (integer division, truncates decimal)
The Modulo Operator
% returns the remainder. It is used for checking divisibility, cycling through lists, and time calculations.
17 % 5 = 2 (17 = 5 × 3 + 2)
Key Takeaways
- All programming arithmetic uses the same rules as written maths.
- / gives a decimal; // gives the whole number quotient.
- % (modulo) gives the remainder.
- Use parentheses to control the order of operations.
Practice Questions
- What is the result of 3 + 5 * 2 in Python?
- What does 17 // 3 return?
- What does 17 % 3 return?
- How would you check in code whether a number n is even?
- Calculate 2 ** 10.
