[Löst] Strong Number - PHP Task

  

3
Ämnesstart

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

1 Answer
2

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';
Dela: