How to check if an integer can be divided by 3

后端 未结 8 1132
不知归路
不知归路 2020-12-29 03:47

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,         


        
8条回答
  •  萌比男神i
    2020-12-29 04:40

    inside the loop:

    if (i%3 == 0)
        // it can be divided by 3
    

    % is called "mod" or "modulus" and gives you the remainder when dividing two numbers.

    These are all true:

    6 % 3 == 0
    7 % 3 == 1
    7 % 4 == 3
    

提交回复
热议问题