PHP Practical Slip 13 Q.B

Write a PHP Script to display a perfect numbers between 1 to100.

<?php
echo "<h3>Perfect Numbers between 1 and 100:</h3>";

for ($num = 1; $num <= 100; $num++) {
    $sum = 0;

    // Find divisors and sum them
    for ($i = 1; $i < $num; $i++) {
        if ($num % $i == 0) {
            $sum += $i;
        }
    }

    // Check if sum equals the number
    if ($sum == $num) {
        echo $num . " ";
    }
}
?>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top