58. PHP Program to find the given number is Perfect number or Not.
How does this program work?
- In this program you are going to learn about to check if a number is Perfect number or not using PHP.
- A perfect number is a number if it is equal to the sum of its factors, which means original number is equal to sum of all its factors except the number itself.
- If sum is equal to the original number, then that number is perfect number else the number is not a perfect number.
Here is the code
<html>
<head>
<title>PHP Program To Check a given number is Perfect number or Not</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter a number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$r = 0;
$sum = 0;
for ($i = 1; $i <= ($n - 1); $i++)
{
$r = $n % $i;
if ($r == 0)
{
$sum = $sum + $i;
}
}
if ($sum == $n)
{
echo "$sum is Perfect number.";
}
else
{
echo "$sum is not a Perfect number.";
}
return 0;
}
?>
</body>
</html>