3
05/11/2024 3:07 pm
Topic starter
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:
1 Answer
2
05/11/2024 3:08 pm
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';