Divisibility by 3 without using % or /

I saw in a placement paper this question. Put me to think.

So here is one solution, but lengthy in execution.
 int divisible_by_3(int num)  
 {  
    int temp = num;  
    while(num>=3)  
     {  
        num -=3;  
     }  
     if(num==0)  
      {  
        return 1;//number is divisible  
      }  
     return 0;//number not divisible  
 }   

Comments

Popular Posts