Loading...
3+
3
Login

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

OperationPython / JSExampleResult
Addition+5 + 38
Subtraction10 − 46
Multiplication*6 * 742
Division/15 / 43.75
Integer division//15 // 43
Modulo (remainder)%15 % 43
Exponentiation**2 ** 8256

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

  1. What is the result of 3 + 5 * 2 in Python?
  2. What does 17 // 3 return?
  3. What does 17 % 3 return?
  4. How would you check in code whether a number n is even?
  5. Calculate 2 ** 10.
HomeAboutResourcesDashboard