PHP Program to check whether the number is Perfect, Abundant or Deficient


March 18, 2022, Learn eTutorial
1459

What are Perfect, Abundant, and Deficient Numbers?

Perfect numbers are those numbers that have the sum of their proper factors equal to their own value. While the numbers that have the sum of their proper factors less than their own value are called Deficient numbers. and the numbers that have the sum of their proper factors greater than their own value are called the Abundant numbers. 

For example, 6 is a Perfect number, 12 is an Abundant number, and 9 is a Deficient number.

How to check whether a number is a Perfect, Abundant, or Deficient number in PHP?

In PHP to check whether a number is a perfect abundant or deficient number first, we have to read the value into the variable and find the perfect divisors of the entered number by performing the operation 'number % i' and assign the value to a variable then we have to add those perfect divisors together and assign the value to a variable after these operations we have to compare the result if it is equal to the entered number then it is a Perfect number or if it is greater then it is Abundant number otherwise it is Deficient number.

ALGORITHM

Step 1: Accept the number into the variable number from the user to check the number is perfect, abundant, or deficient

Step 2: Assign the value 0 into the variable remainder and addition

Step 3: Assign the value of the variable number into the variable flag

Step 4: Perform the for loop and assign the value 1 into the variable i and perform the sub-steps until the condition 'i < number' becomes false and increment the value of variable i in every iteration

        (i) Perform the operation 'number % i' and assign the computed result into the variable reminder

        (ii) Check the condition 'reminder == 0' using the if statement and if it is true perform the operation 'addition += i'

Step 5: Check the condition 'additi flag' using if statement it true print '-The inputted number is Perfect' using echo and exit the program otherwise go to Step 6

Step 6: Check the condition 'addition > flag' using else if statement it true print '-The inputted number is Abundant' using echo and exit the program otherwise go to Step 7

Step 7: Print '-The inputted number is Deficient' using echo  

PHP Source Code

                                          <?php
$number = readline("Enter the number: ");
$remainder = 0; $additi
$flag = $number;
for ($i = 1; $i < $number; $i++) {
    $remainder = $number % $i;
    if ($remainder == 0) {
        $addition += $i;
    }
} if ($additi $flag) {
    echo "-The inputted number is Perfect";
} else if ($addition > $flag) {
    echo "- The inputted number is Abundant";
} else {
    echo "-The inputted number is Deficient";
}
?>
                                      

OUTPUT

Example 1
Enter the number: 28
-The inputted number is Perfect

Example 2
Enter the number: 36
- The inputted number is Abundant

Example 3
Enter the number: 26
-The inputted number is Deficient