Write a PHP program to check whether a number is an Armstrong number or not. Return true if the number is Armstrong otherwise return false
- برمجة بي اتش بي
- 2021-09-08
- mhanasmh00489829403
الأجوبة
<?php
function armstrong_number($num) {
$sl = strlen($num);
$sum = 0;
$num = (string)$num;
for ($i = 0; $i < $sl; $i++) {
$sum = $sum + pow((string)$num{$i},$sl);
}
if ((string)$sum == (string)$num) {
return "True";
} else {
return "False";
}
}
echo "Is 153 Armstrong number? ".armstrong_number(153);
echo "\nIs 21 Armstrong number? ".armstrong_number(21);
echo "\nIs 4587 Armstrong number? ".armstrong_number(4587);"\n";
?>
Sample Output:
Is 153 Armstrong number? True Is 21 Armstrong number? False Is 4587 Armstrong number? False
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال