A perfect number is a number if it is equal to the sum of its factors,that is original number is equal to sum of all its factors excluding the number itself. We have already discuss how to check if a number is perfect or not in this article. In this article we will discuss about how to do the same in PHP.
Examples:
function isPerfectNumber($N)
{
$sum = 0;
for ($i = 1; $i < $N; $i++)
{
if ($N % $i == 0)
{
$sum = $sum + $i;
}
}
return $sum == $N;
}
$N = 6;
if (isPerfectNumber($N))
echo " Perfect Number";
else
echo "Not Perfect Number";
?>