This is a basic PHP program which describes how to write Armstrong Number program in PHP. Below examples will help you in the better understanding of Armstrong Number concept in PHP programming language.
We are using if…else statement and while loop to check whether the entered number is Armstrong number or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<html> <head> <title>Armstrong number in PHP</title> </head> <body> <form method="post"> Enter a number: <input type="number" name="number"> <input type="submit" value="Submit"> </form> </body> </html> <?php /** * Armstrong number in PHP */ if($_POST) { //get the number entered $number = $_POST['number']; //store entered number in a variable $a = $number; $sum = 0; //run loop till the quotient is 0 while( $a != 0 ) { $rem = $a % 10; //find reminder $sum = $sum + ( $rem * $rem * $rem ); $a = $a / 10; //find quotient. if 0 then loop again } //if the entered number and $sum value matches then it is an armstrong number if( $number == $sum ) { echo "Yes $number an Armstrong Number"; }else { echo "$number is not an Armstrong Number"; } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.