You do this using the modulus operator, %
n % k == 0
evaluates true if and only if n
is an exact multiple of k
. In elementary maths this is known as the remainder from a division.
In your current approach you perform a division and the result will be either
- always an integer if you use integer division, or
- always a float if you use floating point division.
It’s just the wrong way to go about testing divisibility.