PHP Program to check whether a number is a disarium number or not

How to check whether a number is a disarium number or not?

In this program we are checking the number is disarium or not. To check that we have to find the power of each digit of the number according to their position and add those together and if the result is equal to the original number then the number is considered as disarium number. For example, if the entered number is 135 then we calculate 11 + 32 + 53 which is 135 as we can see that the result is the same as the original number so we can say that 135 is a disarium number.

How to check whether a number is a disarium number or not using PHP?

In this program, we are accepting the value from the user and assigning it to the variable num. Then we have to assign value 0 into the variables sum and rem and also assign the length of the number into the variable size and assign the value of the variable num into the variable n. After that, we have to perform while loop until the condition 'n > 0' becomes false and in the block of the loop first assign the computed values of 'n % 10' into the variable rem, 'sum + pow(rem, size)' into the variable sum and 'n /10' into the variable n and also decrement the value of the variable size and after the completion of the loop we have to check the condition 'sum == num' if true print the value of the variable as disarium number otherwise as not a disarium number.

ALGORITHM

Step 1: Accept the number from the user into the variable num

Step 2: Assign value 0 into the variables sum and rem

Step 3: Assign the length of the number into the variable size

Step 4: Assign the value of the variable num into the variable n

Step 5: Perform sub-steps until the condition 'n > 0' becomes false

        (i) Assign the computed value of 'n % 10' into the variable rem

        (ii) Assign the computed value of 'sum + pow(rem, size)' into the variable sum

        (iii) Assign the computed integer value of 'n /10' into the variable n

        (iv) Decrement the value of the variable size

Step 6: Check the condition 'sum == num' if true print the value of the variable as disarium number otherwise as not a disarium number

PHP Source Code

                                          <?php
$num = readline("Enter the number");
$rem = 0;
$sum = 0;
$size = strlen($num);
$n = $num;
while ($n > 0) {
    $rem = $n % 10;
    $sum = $sum + pow($rem, $size);
    $n = (int)($n / 10);
    $size--;
}
if ($sum == $num)
    echo "$num is a disarium number";
else
    echo "$num is not a disarium number";
?>
                                      

OUTPUT

Example 1
Enter the number518
518 is a disarium number

Example 2
Enter the number143
143 is not a disarium number

Example 3
Enter the number89
89 is a disarium number