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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. <?php
  2. function armstrong_number($num) {
  3. $sl = strlen($num);
  4. $sum = 0;
  5. $num = (string)$num;
  6. for ($i = 0; $i < $sl; $i++) {
  7. $sum = $sum + pow((string)$num{$i},$sl);
  8. }
  9. if ((string)$sum == (string)$num) {
  10. return "True";
  11. } else {
  12. return "False";
  13. }
  14. }
  15. echo "Is 371 Armstrong number? ".armstrong_number(371);
  16. echo "<br>Is 21 Armstrong number? ".armstrong_number(8208);
  17. echo "<br>Is 4587 Armstrong number? ".armstrong_number(10);"\n";

Result

python language techstudy.org

     





Related Topics




Popular Posts