Display Prime Number

Follow by Email
Facebook
Google+
http://xtremepassion.leladystore.com/2011/02/display-prime-number/
YouTube
PINTEREST
INSTAGRAM

Display prime numbers up to 50.

PHP Code (Save As displayprimeno.php):

<?php

for($i=2; $i<=50; $i++){
   $flag=0; //isPrime
   for($j=2; $j<=$i/2; $j++){
      if(($i%$j)==0){
         $flag=1; //notPrime
         break; //get out from the loop
      }
   }
   if($flag==0)
      echo $i . " is prime no"; //displayed
}

?>

Results (http://localhost/displayprimeno.php):

2 is prime no
3 is prime no
5 is prime no
7 is prime no
11 is prime no
13 is prime no
17 is prime no
19 is prime no
23 is prime no
29 is prime no
31 is prime no
37 is prime no
41 is prime no
43 is prime no
47 is prime no

*ENJOY CODING IN PHP!*

Leave a Reply

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

1,189 views