Program to find prime numbers using PHP

This is a simple program in PHP to find prime numbers using for loop and if  statement.

<?php
error_reporting(E_ALL);
//Program to find prime numbers in between a range

$num =23;

for( $j = 2; $j <= $num; $j++ )
{
for( $k = 2; $k < $j; $k++ )
{
if( $j % $k == 0 )
{
break;
}

}
if( $k == $j )
echo “Prime Number : “, $j, “<br>”;
}

OUTPUT

Prime Number : 2
Prime Number : 3
Prime Number : 5
Prime Number : 7
Prime Number : 11
Prime Number : 13
Prime Number : 17
Prime Number : 19
Prime Number : 23

 

Advertisements
Posted in PHP

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s