Check if number is divisible by 24 [closed]

我怕爱的太早我们不能终老 提交于 2020-01-12 11:10:00

问题


I'd like to put up a if function, that will see if the variable is dividable by 24, if it's then it does the function else not, same logic however, I want to see if the output is a perfect number, e.g if we do 24/24 that will get 1, that's a perfect number. If we do 25/24 then it'll get 1.041 which is not a perfect number, the next perfect number will come when it reaches 48 that will be 48/24 which will get 2 that's a perfect number.


回答1:


Use the Modulus operator:

if (number % 24 == 0)
{
   ...
}

The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.

Pretty much it returns the remainder of a division: 25 % 4 = 1 because 25 fits in 24 once, and you have 1 left. When the number fits perfectly you will get a 0 returned, and in your example that is how you know if a number is divisible by 24, otherwise the returned value will be greater than 0.




回答2:


How about using Modulus operator

if (mynumber % 24 == 0)
{
     //mynumber is a Perfect Number
}
else
{
    //mynumber is not a Perfect Number
}

What it does

Unlike / which gives quotient, the Modulus operator (%) gets the remainder of the division done on operands. Remainder is zero for perfect number and remainder is greater than zero for non perfect number.



来源:https://stackoverflow.com/questions/19395334/check-if-number-is-divisible-by-24

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!