Strong Number Program in PHP
In this article, you will learn how to check strong number using PHP programming language.
In mathematics, Factorial is the product of all positive integers less than or equal to a given positive integer and denoted by that integer and an exclamation point. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as -
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
The factorial operation generally comes across many mathematical operations, like - combinatorics, algebra, analysis.
A strong number is a number in which the sum of the factorial of the digits is equal to the number itself. Like - 1, 2, 145, 40585 and so on. Example -
Input Number - 145
Sum of Factorial of the digits -
1! + 4! + 5!
1 + 24 + 120
145 // Strong Number
As you can show above, the input number is equal to the sum of the factorial of the digits. So, number 145 is the factorial number. Again, let's check for the other number -
Input Number - 162
Sum of Factorial of the digits -
1! + 6! + 2!
1 + 720 + 2
723 // Not a Strong Number
PHP Strong Number Program Source Code
Here is the program to check whether the input number is a strong number or not in PHP.
<!doctype html>
<html>
<head>
<title>Strong number program in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Strong number program in PHP</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="" required>
</div>
<button type="submit" name="submit" class="btn btn-default">Submit</button>
<?php
if(isset($_POST['number'])){
echo "<br><br>";
if(checkStrongNumber($_POST['number'])){
echo $result = $_POST['number']." is strong number program.";
} else {
echo $result = $_POST['number']." is not a strong number program.";
}
}
?>
</form>
</div>
</body>
</html>
<?php
function checkStrongNumber($number)
{
$fact;
$x = $number;
$sum = 0;
while ($number != 0)
{
$fact = 1;
for($i = 1; $i <= $number % 10; $i++)
$fact *= $i;
$sum += $fact;
$number = (int)($number / 10);
}
return $sum == $x;
};
?>
Related Articles
Fibonacci Series Program in PHPPHP Prime Number Program
How to lock a file using PHP
PHP FTP server connection and file handling
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP random quote generator
PHP calculate percentage of total
PHP sanitize input for MySQL
PHP import Excel data to MySQL using PHPExcel
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP