Check if number is divisible by 24
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.
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.