57. PHP Program to find the given number is Prime number or Not.
How does this program work?
- In this program you are going to learn about to check if a number is Prime number or not using PHP.
- Prime number means which is only divisible by 1 and that number itself, and cannot divisible by any other number.
- When the number is divided by 2, we use the remainder operator % to compute the remainder then flag will be effected.
- If the flag is zeo, the number is prime number else the number is not a prime.
Here is the code
<html>
<head>
<title>PHP Program To Check a given number is Prime number or Not</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter positive interger"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$flag = 0;
for($i = 2; $i <= $n/2; $i++)
{
if($n%$i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo "$n is a prime number";
else
echo "$n is not a prime number";
return 0;
}
?>
</body>
</html>