A perfect number is a positive integer where the sum of all its positive
divisors, except itself, is equal to the number itself. For example 6
is a perfect number as 1,2 and3 are its divisors and the sum of
divisors=1+2+3=6. Here we have created a program that will take the
number from the user and reports whether it is perfect or not
<?php
function perfectnmb($num){
$perfectNo =0;
for ($i = 1;$i < $num; $i++) {
if ($num % $i == 0) {
$perfectNo += $i;
}
}
if ($perfectNo == $num) {
return true;
} else {
return false;
}
}
for($i=1;$i<=100;$i++){
if(perfectnmb($i)){
echo "perfect number are ".$i."<br>";
}
}
<?php
function perfectnmb($num){
$perfectNo =0;
for ($i = 1;$i < $num; $i++) {
if ($num % $i == 0) {
$perfectNo += $i;
}
}
if ($perfectNo == $num) {
return true;
} else {
return false;
}
}
for($i=1;$i<=100;$i++){
if(perfectnmb($i)){
echo "perfect number are ".$i."<br>";
}
}
No comments:
Post a Comment