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.
If you are divisible by both 6 and 4, the number could be 12 which is not divisible by 24. The reason for using 3 and 8 is that the least common multiple is 24. So every number that is divisible by both 3 and 8 is divisible by 24.
As you have probably figured out by now, the list of numbers divisible by 24 is infinite. Here is the beginning list of numbers divisible by 24, starting with the lowest number which is 24 itself: 24, 48, 72, 96, 120, 144, 168, 192, 216, 240, etc. As you can see from the list, the numbers are intervals of 24.
For example: Is 24 divisible by 4? Yes, because when we divide 24 by 4, the quotient is 6 and the remainder is 0.
However divisibility by 3 is not so simple - the rule is to add up all of the digits of the number and if they are a multiple of 3 then the original number is also. What is the divisibility rule of 24? If the number is separately divisible by 3 and 8 then the number is divisible by 24.
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 % 24 = 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With