Home / PHP / Program To Find Prime Number in PHP

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 ,23, and 111. 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. Here we will write a program to find prime number in PHP.

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 given 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 2 to n/2, 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();

 

Learn How to Connect PHP with MySQL Database and Create Database Table

 

About Abid

I am Abid Hussain a professional software developer, worked on dozens of projects specializing in web development. I also provide web development service on Fiverr and Freelancer.

Check Also

install laravel using xampp

How To Install Laravel Framework on Your Computer

Install Laravel 5 on Your Computer using XAMPP Laravel is an open source and free …

Leave a Reply

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