In this lesson you are going to learn how to find if the entered number is an Armstrong number or not in PHP. This program is for school and college kids who want to learn something new on PHP.
Armstrong number are 371, 407 etc.,
Here is the entire PHP script:
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 43 |
<html> <body> <!-- to test this script type 371 or 407, these are armstrong numbers --> <h2>Find Armstrong Number - PHP Script by <a href="http://www.tutorialsmade.com/php-script-find-armstrong-number/">Tutorialsmade.com</a></h2> <form action="" method="post"> <input type="text" name="number" /> <input type="submit" /> </form> </body> </html> <?php if( $_POST ) { //get the enter number $number = $_POST[ 'number' ]; //store it in a temp variable $temp = $number; $sum = 0; //loop till the quotient is 0 while( $temp != 0 ) { $rem = $temp % 10; //find reminder $sum = $sum + ( $rem * $rem * $rem ); //cube the reminder and add it to the sum variable till the loop ends $temp = $temp / 10; //find quotient. if 0 then loop again } //if the entered number and the $sum value matches then it is an armstrong number if( $number == $sum ) { echo "Armstrong Number"; }else { echo "Not an Armstrong Number"; } } ?> |
You can easily understand this code by reading the comments in it.
The output will be either Armstrong Number or Not an Armstrong Number
To try out this script I have given a demo link below:
Hello friend
hello,hope this information is helpful to you