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

Introduction

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

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

Leave a Comment