C# How to determine if a number is a multiple of another?
Try
public bool IsDivisible(int x, int n)
{
return (x % n) == 0;
}
The modulus operator % returns the remainder after dividing x by n which will always be 0 if x is divisible by n.
For more information, see the % operator on MSDN.
bool isMultiple = a % b == 0;
This will be true if a is a multiple of b
Use the modulus (%
) operator:
6 % 3 == 0
7 % 3 == 1