settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Strong Number - PHP Task

+7 votes

Write a program to check if a given number is a strong number or not. A number is strong if the sum of the Factorial of each digit is equal to the number. For example 145 is a strong number, because 1! + 4! + 5! = 145.

Print "yes" if the number is strong and "no" if the number is not strong.

Examples:

strong number php task

asked in PHP category by user samfred5830

1 Answer

+6 votes

Here's my solution - you can use strlen function:

<?php

$number = readline();
$sumFactorial = 0;

for ($i = 0; $i < strlen($number); $i++) {
    $number[$i];
    $currFactorial = 1;

    for ($n = $number[$i]; $n >= 1; $n--) {
        $currFactorial *= $n;
    }

    $sumFactorial += $currFactorial;
}

echo ($sumFactorial == $number) ? 'yes' : 'no';
answered by user icabe
...