Home >>PHP Programs >Check A Number is Prime or Not in PHP

Check A Number is Prime or Not in PHP

How to Check a Number is Prime or Not in PHP

If you are looking to check a number is prime in PHP then you are at the right place because here we will show whether a number is prime or not using PHP.

Here I am just showing you a program to check prime number in php. The technique that I am going to show you very simple and simple but quite potent and here you will also find how to alter and personalize these codes readily according to your need.

What is Prime Number?

A Number that is divisible by itself as well as 1 then it called given number is Prime Number. For eg- in the event that you take the number 11, it can only be divided to acquire a complete number if it is divided by 1 or 11. If any other number is used subsequently a percentage is obviously found.

<?php 
if(isset($_POST['submit']))
{
//retrieve number form POST submission
	$num=$_POST['num'];
	$primeFlag=0;

	//if not perfectly divisible by any,
	//number is prime
		for($i=2;$i<$num;$i++)
		{
			$primeFlag=0;
			if(($num%$i)==0)
			{
			break;	
			}
			$primeFlag=1;
		}	
		if($primeFlag==1)
		{
			echo " $num  is Prime Number";
		}
		else
		{
			echo "$num is not  Prime number";	
		}
}
?>
<!DOCTYPE html?>
<html?>
<head>
	<title>Write a Program to check Prime Number</title>
</head>
<body>
	<form method="post">
			
		<table>
			<tr>
				<td>Enter Your Number</td>
				<td><input type="text" name="num"></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" name="submit" value="submit">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
Output:

Check Prime Number in Array

<?php 
if(isset($_POST['submit']))
{
//retrieve number form POST submission
//convert to array by splittiing on comma
	$numStr=$_POST['num'];
	$numArr=explode(",",$_POST['num']);
	$primes=array();
	$primeFlag=0;

	//iterate over array
	//get absolute values of each number
	foreach($numArr as $n)
	{
	$n=trim(abs($n));
	//test each number for prime-ness:
	//check the number by dividing it
	//by all the numbers between 2 and itself
	//if not perfectly divisible by any,
	//number is prime
		for($i=2;$i<$n;$i++)
		{
			$primeFlag=0;
			if(($n%$i)==0)
			{
			break;	
			}
			$primeFlag=1;
		}	
		//if prime add to output array
		if($primeFlag==1)
		{
		array_push($primes,$n);	
		}
	}

	//check if any primes number were found then sort and remove duplicate from array and prit
	if(count($primes)>0)
	{
		$primes=array_unique($primes);
		sort($primes);
		echo "The following numbers are prime : ".implode($primes," ");	
	}
	else
	{
	echo "No Prime numbers found";	
	}

}
?>
<!DOCTYPE html>
<html>
<head>
	<title>check prime number</title>
</head>
<body>
	<form method="post">
			
		<table>
			<caption><h3>Project : Prime Number Tester</h3></caption>
			<tr>
				<td>Enter a list of numbers, Seperated by Commas</td>
				<td><input type="text" name="num"></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" name="submit" value="submit">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

Total Downloads : 6

Login / Register To Download

No Sidebar ads