Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an integer can be divided by 3

Tags:

java

How to check if my integer can be divided by 3 as below:

for(int i=0; i<24;  i++){    //here, how to check if "i" can be divided by 3 completely(e.g. 3, 6, 15)?  } 
like image 654
Leem Avatar asked Jul 11 '11 08:07

Leem


People also ask

How do you check if a number is divisible by a number?

A number is divisible by another number if it can be divided equally by that number; that is, if it yields a whole number when divided by that number. For example, 6 is divisible by 3 (we say "3 divides 6") because 6/3 = 2, and 2 is a whole number.

Why is a number divisible by 3?

What is the Divisibility Rule of 3 and 4? According to the divisibility rule of 3, a number is said to be divisible by 3 if the sum of all digits of that number is divisible by 3. For example, the number 495 is completely divisible by 3. The sum of all digits are 4 + 9 + 5 = 18 and 18 is divisible by 3.

Can an integer be divided?

Division of integers involves the grouping of items. It includes both positive numbers and negative numbers. Just like multiplication, the division of integers also involves the same cases. When you divide integers with two positive signs, Positive ÷ Positive = Positive → 16 ÷ 8 = 2.


1 Answers

Use the modulo operator.

if(i % 3 == 0) 

Also see Modulo operation at Wikipedia

like image 147
Sebastian Wramba Avatar answered Sep 19 '22 10:09

Sebastian Wramba