Logic to check the number is divisible by 3 or not?

后端 未结 10 1412
清歌不尽
清歌不尽 2021-01-14 16:31

without using %, / or * , I have to find the no. is divisible by 3 or not?

it might be an interview question.

Thanks.

10条回答
  •  醉梦人生
    2021-01-14 17:07

    Subtract 3 until you either

    hit 0 - number was divisible by 3 (or)

    get a number less than 0 - number wasn't divisible

     if (number > 0)
     {
            while (number > 0)
            {
                number -= 3;
        }
    }
    else if( number < 0)
    {
        while number < 0:
            number += 3
    }
    return number == 0
    

提交回复
热议问题