How to check if an integer can be divided by 3

后端 未结 8 1083
不知归路
不知归路 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条回答
  • 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
    
    0 讨论(0)
  • 2020-12-29 04:42
    if( i % 3 == 0 ){
    System.out.println("can be divided by 3");
    }else{
    System.out.println("cant divide by 3");
    }
    

    Is this question for real?

    0 讨论(0)
提交回复
热议问题