Checking Prime Numbers: PHP

To know the number is prime or not using the programming language PHP, we can use the following method:

<?php
$n = 4; // please enter the value of N here
$status = “PRIME”;
for ($i = 2; $i <= $n-1; $i++)
{
if ($n % $i == 0)
{
$status = “NO PRIME”;
break;
}
}
echo “Number “.$n.” is : “.$status;
?>

$ n = 4; // here you are free to determine what the value or number you want to check, here I am trying to check the status of number 4. if prime prime prime not otherwise status.

the result:

Checking Prime Numbers: PHP

4 is not a prime number

is now $n I change into 2

results:

Checking Prime Numbers: PHP
2 is a prime number

If you have any questions please comment.
Do not forget to like and share.
Learn Programming in fajaryusuf.com wrote. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *