Check if a number is divisible by 3

后端 未结 16 2921
予麋鹿
予麋鹿 2020-12-01 01:30

I need to find whether a number is divisible by 3 without using %, / or *. The hint given was to use atoi() function. Any

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 02:17

    well a number is divisible by 3 if all the sum of digits of the number are divisible by 3. so you could get each digit as a substring of the input number and then add them up. you then would repeat this process until there is only a single digit result.

    if this is 3, 6 or 9 the number is divisable by 3.

提交回复
热议问题