Prime Number Program in PHP

Hello everyone, today I am going to teach the logic of basic prime number in php. Its very simple. First of all, we need to know what are prime numbers? Prime numbers are those numbers which are divisible by 1 and its own number. Like 1,3,5,7,11... so on these are all prime numbers. So how we can make logic for prime number program.

Let's take an example, suppose take a number 7 and find that this number is prime or not. We all know that 7 is divisible by 1. We have to test divisible one by one number that comes after 1 or one less than a given number.

Like: 2,3,4,5,6, if 7 is not divisible by any of these numbers then it is prime otherwise not.  The number which is divisible by any number which is one less than the given number except 1 then it is not a prime number.

Program in php:

<?php

function IsPrimeNumber($num)
{
  for($i=2; $i<$num; $i++)
    {
      if($num%$i == 0)
      {
   return false;
  }
}
return true;
}



if (IsPrimeNumber(3)) {
echo 'This is a Prime Number..'."\n";
} else {
echo 'This is not a Prime Number.....'."\n";
}

Comments

Popular posts from this blog

Laravel 5.8 Files Folders Permissions and Ownership Setup

20 excellent contact pages

Nivo Lightbox jQuery Plugin