SitePoint Sponsor

# Thread: Check if a number is a multiple of 3

1. ## Check if a number is a multiple of 3

How would I check if a number is a multiple of 3?

2. var num = 9;
if (num &#37; 3 == 0) {
alert(' its divisible by 3 ');
} else {
alert('its not divisible by 3');
}
This should do it.

3. You could shorten the if test to

if (!num&#37;3) alert('divisible by 3');

4. Yes it could be shortened to that but it becomes harder to make adjustments to, depending on the needs of the author

5. Originally Posted by felgall
You could shorten the if test to

if (!num&#37;3) alert('divisible by 3');
Basically 0 converts to boolean FALSE. So you can just check for false (using the NOT operator) instead of 0.

6. While completely useless in answering your question, you can tell if a number is divisible by 3 by adding all the digits together. If the new number is divisible by 3, so is your original number. The same is true for 9.

7. Originally Posted by fattyjules
While completely useless in answering your question, you can tell if a number is divisible by 3 by adding all the digits together. If the new number is divisible by 3, so is your original number. The same is true for 9.
0 is divisible by 3, so an exception would have to be made for that rule. 6 and 9 break that rule too.

8. Originally Posted by pmw57
0 is divisible by 3, so an exception would have to be made for that rule. 6 and 9 break that rule too.
The rule only works for positive integers. How do 6 and 9 break the rule?

9. I was thinking of something else, never mind.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•