Q. PHP program to Check Whether a Number is Prime or not.

Here you will find an algorithm and program in PHP to check whether the given number is prime number or not. First let us understand what prime number means.

Explanation : A number that is divisible by only and only 1 and itself is known as a Prime Number. For example: - 11 is only divisible by 1, so 11 is prime, while 10 is divisible by 1, 2, and 5 so 10 is not a prime number.

Prime Number Algorithm

START
  Step 1 → Take integer variable A
  Step 2 → Divide the variable A with (A/2 to 2)
  Step 3 → If A is divisible by any value (A/2 to 2) then it is not prime
  Step 4 → Else it is prime number
STOP


PHP Program to Check Whether a Number is Prime or not

function primeCheck($number){ 
if ($number == 1) 
return 0; 
for ($i = 2; $i <= $number/2; $i++){ 
if ($number % $i == 0) 
return 0; 
} 
return 1; 
} 
$number = 31; 
$flag = primeCheck($number); 
if ($flag == 1) 
echo "31 is a prime number."; 
else
echo "31 is Not a prime number."

Output

31 is a prime number