Q:

Write a PHP program to check if a number is an Armstrong number or not

belongs to collection: PHP basic programs with examples

0

Write a PHP program to check if a number is an Armstrong number or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

PHP is a server scripting language, and It is a powerful tool for making interactive and dynamic Web-pages. I have used WampServer 2.2 for following excercise..

 <?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 371 Armstrong number? ".armstrong_number(371);
echo "<br>Is 21 Armstrong number? ".armstrong_number(8208);
echo "<br>Is 4587 Armstrong number? ".armstrong_number(10);"\n";

Result:

Is 371 Armstrong number? True
Is 21 Armstrong number? True
Is 4587 Armstrong number? False

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a PHP program to convert word to digit... >>
<< Write a PHP program to swap two variables...