How to Check Whether a Number Is Divisible by Another Number

How do you check whether a number is divisible by another number?

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.

Check whether a number is divisible by another number If not make it divisable in Javascript

You can do that by using a simple while loop:

function doThat(number, divider) {
while(number % divider !== 0) {
number++;
}
return number;
}

doThat(12, 5); // => returns 15

Here's a fiddle: https://jsfiddle.net/rdko8dmb/

How to check if a number if divisible by two different values?

Your expression is asking "Is year a multiple of four or is it a multiple of 100 and also a multiple of 400?" The second half is entirely redundant because any multiple of both 100 and 400 was already a multiple of 4 in the first place (same with 400 and 100 also) and the result is clearly more inclusive than you intended.

Remember that a series of AND'd conditions will restrict the scenarios that match while ORs will broaden it. So start with finding multiples of four and then refine that a bit by adding in the secondary condition about multiples of 100.

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) /* right! */

Since A(B+C) is equivalent to AB+AC in Boolean logic you could also expand this and it might read more clearly to you:

(year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 year % 400 == 0) /* expanded */

The parentheses aren't really necessary but I left them for clarity.

Another perspective that might help you think about this is reversing the condition to find the years that aren't leap years. It's very similar in structure to the one you attempted so I think that studying it might give you some insight where your thought process went wrong:

year % != 0 || (year % 100 == 0 && year % 400 != 0) /* NON-leap years */

Finally, one last thing to point out is that it only takes either side of an OR to be true for the whole expression to be true. Looking back at your original logic, once the program has determined that year % 4 == 0 is true the rest of it is extra work that doesn't need to be performed. The program will actually skip those steps and this concept is called short-circuiting.

Fastest way to check if a number is divisible by another in python

What about:

return (number % divisor == 0)

How to check if number is divisible by a certain number?

n % x == 0

Means that n can be divided by x. So... for instance, in your case:

boolean isDivisibleBy20 = number % 20 == 0;

Also, if you want to check whether a number is even or odd (whether it is divisible by 2 or not), you can use a bitwise operator:

boolean even = (number & 1) == 0;
boolean odd = (number & 1) != 0;

Find if a number is divisible by another number using recursion

For educational purposes, you could keep subtracting until you reach 0 or below:

def is_divisible(num, div):
while(num > 0):
num -= div
return num == 0

Using this logic you can make it recursive (Thanks to Anonymous for pointing out the above version is not recursive):

def is_divisible(num, div):
if (num == 0):
return True
elif (num < 0):
return False
return is_divisible(num-div, div)

However, the correct way would be:

def is_divisible(num, div):
return num % div == 0


Related Topics



Leave a reply



Submit