loader image

PHP Program to check perfect number

Write a PHP program to check whether a given number is perfect, abundant or deficient.

<html>

<body>
    <h2>Perfect, Abundant or Deficient</h2>
    <form action="" method="post">
        Enter the number: 
        <input type="text" name="number" />
        <input type="submit" />
    </form>
</body>

</html>
<?php
if ($_POST) {
    $no = $_POST['number'];
    $sum = 0;
    for ($i = 1; $i < $no; $i++) {
        if ($no % $i == 0)
            $sum = $sum + $i;
    }
    if ($sum == $no)
        echo "Perfect Number";
    else if ($sum > $no)
        echo "Abundant Number";
    else
        echo "Deficient Number";
}
?>
Output
perfect php
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top