Program To Find Prime Number in PHP

A Program To Find Prime Number in PHP

A Prime Number is a whole number greater than 1 , which can only be divided by itself and 1. Some prime numbers are : 3, 5, 7, 11 ,13,17, 19 and 23. These numbers can only be divided by 1 and itself. For example 7 can only be divided by 1 and 7 hence it is a prime number. But 8 can be divide by 4 , 2 , 1 and itself. So it is not a prime number its composite number.

Algorithm to Write Prime Number in PHP

There are many algorithms to write prime number in PHP. Here I am writing a simple algorithm.

Algorithm

  • Create a function.
  • Get number from user to check.
  • Divide number by 2 and store it in a variable name it resultant variable or whatever you like.
  • Run for loop up to resultant .
  • Inside for loop divide number by loop integers and assign each remainder to a variable.
  • If any remainder is equal to zero it is not prime
  • Else it is a prime number.

Write a Program To Find Prime Number in PHP

 




function prime() //Create a Function
{
$n=11; //Number to check
$a=$n/2;
for($i=2;$i<=$a;$i++)
{
$m=$n%$i;
}
if($m==0)
{
echo"is Not Prime";
} else
{
echo "Prime";
}
}
prime();

 

 

Leave a Reply

error: Content is protected !!