In this post we are going to write a PHP program to check Strong number. Before that if you don’t know what is a Strong number here is a small definition.
What is Strong number?
if sum of the factorial of its digit is equal to number itself is called ‘Strong number’.
Eg: 145
145 is a strong number.
1! + 4! + 5! = 1+24+120 = 145
Let’s write a PHP program to find if the entered number is a strong 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 |
<?php //function to check if the number is strong number or not function is_strong_number($num) { $sum = 0; $r = 0; //store the entered number in a temp variable $temp=$num; //split the number and find factorial while($num){ $i=1; $fact=1; //get one digit from $r=$num%10; //factorial script while($i<=$r){ $fact=$fact*$i; $i++; } //sum the factorial value $sum=$sum+$fact; //split the next number $num=floor($num/10); } //check the sum value and stored temp value //if same then it's a strong number and vice-versa if($temp == $sum){ return true; }else{ return false; } } ?> |
The above script will return true if the entered number is a Strong number else false.
Just go through the inline comments in the above script to understand what each line is doing.
Let’s write the whole script so you can copy paste and test it in your localhost immediately:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<html> <head> <title>PHP Program to check Strong number!</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"> </head> <body> <div class="container"> <br /> <h1>PHP Program to check Strong number!</h1> <br /> <form action="" method="post"> <div class="form-group"> <label for="name">Enter number to check</label> <input type="number" class="form-control" name="number" value="<?=(isset($_POST['number']))? $_POST['number']:'';?>" required> </div> <button type="submit" name="submit" class="btn btn-default">Submit</button> <?php if(isset($_POST['number'])){ echo "<br><br>"; if(is_strong_number($_POST['number'])){ $result = "yes"; }else{ $result = "no"; } echo "<b>Result:</b> $result"; } ?> </form> </div> </body> </html> <?php //function to check if the number is strong number or not function is_strong_number($num) { $sum = 0; $r = 0; //store the entered number in a temp variable $temp=$num; //split the number and find factorial while($num){ $i=1; $fact=1; //get one digit from $r=$num%10; //factorial script while($i<=$r){ $fact=$fact*$i; $i++; } //sum the factorial value $sum=$sum+$fact; //split the next number $num=floor($num/10); } //check the sum value and stored temp value //if same then it's a strong number and vice-versa if($temp == $sum){ return true; }else{ return false; } } ?> |
I have used Bootstrap CSS to get a nice look and feel.
For your convenience here is the demo of the above script.
Enjoy learning!