php

PHP prime and composite number by is_init()

A prime number = exactly divisible by same number and by 1 only  eg. 5 divisble by 5 and 1 only

A composite number = number exactly divisible  by other number  other  than one and itsself. eg . 4  divided by 2 also ther than 4,1.

Special PHP function used is  is_int() It returns true or false depending on variable value is integer or not

<?php
for($i=2;$i<18;$i++)
{
$count = 0;
$z = $i/2;
if(is_int($z)){
for($j=2;$j<=$i/2;++$j)
{
if($i%$j==0){
$count=$count+1;
}
}
}
else{
$z=explode(‘.’,$z);
$y = $z[0];
for($j=2;$j<=$y;++$j)
{
if($i%$j==0){
$count=$count+1;
}
}
}
if ($count==0)
{ echo $i.”is prime <br>”;}
else
{ echo $i.”is composite <br>”; }
}
?>

?>

OUTPUT2is prime 
3is prime 
4is composite 
5is prime 
6is composite 
7is prime 
8is composite 
9is composite 
10is composite 
11is prime 
12is composite 
13is prime 
14is composite 
15is composite 
16is composite 
17is prime 

Leave a Reply

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