How to find whether a number is prime or not using PHP
What is Prime Number?
A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
In this tutorial, we will learn how to find whether a number is prime or not in PHP.
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 |
<?php if(isset($_POST['submit'])) { $check=0; $num=$_POST['num']; for($i=2;$i<=($num/2);$i++) { if($num%$i==0) { $check++; if($check==1) { break ; } } } if($check==0) { echo "It is a Prime Number"; } else { echo "It is not a Prime Number"; } } ?> <!DOCTYPE html> <html> <head> <title>Check whether a number prime or not</title> </head> <body> <form name="primenumber" action="" method="post"> Number :<input type="text" name="num" value="" required><br> <input type="submit" value="Submit" name="submit"> </form> </body> </html> |