Showing posts with label prime number generator in php. Show all posts
Showing posts with label prime number generator in php. Show all posts

Thursday, January 8, 2015

Prime Number Generator in PHP

As a programmer I always learns how to apply my previous knowledge in other programming language before I wrote a program that will generate prime numbers in C and C++ programming language. The program works perfectly in my programming class. Recently I am more involved in wed design and development my main primarily language of choice in PHP so I have decide why not rewrote my code from C/C++ into its PHP equivalent here is it a prime number generator written in PHP. 

As we know a prime number is a number that is divisible by one and only itself example of prime numbers are 2,3,5.7,11,13 and 17 etc. In this sample program I make a form which the user can enter a number and then their are two buttons the first one will check if the number is a prime number or not and the other button will clear the text box and the results in our webpage. I also incorporate autofocus as a function in my program this function is available in HTML 5 to make the cursor focus in our text field. My program will also display the list of numbers that are not prime in terms of their values.

I hope you will find my work useful and beneficial if you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.

Thank you very much and Happy Programming.



Sample Output of Our Program

Program Listing

<html>
<title> Prime Number Generator </title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
</style>
<?php

$values = $_POST['value'];

if(isset($_POST['clear'])) {
  $title=""; $a="";
  $values=" ";
}
?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
  <br> <h1 align="center">Prime Number Generator</h2>
  <br>
<form action="" method="post">
 Enter a Number : <input type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=20/>
   <input type="submit" name="check" value="Check Prime Number" 
  title="Click here to view the list of prime numbers"/>
  <input type="submit" name="clear" value="Clear" 
  title="Click here to clear text box and values on the screen"/>
</form>


<?php

if(isset($_POST['check'])) {

$title ="List of Prime Numbers";
$title2="List of Non-Prime Numbers";

echo $title;
echo "<br><br>";
for( $a = 2; $a <= $values; $a++ )
{
   for( $b = 2; $b < $a; $b++ )
   {
     if( $a % $b == 0 )
      {
        break;
      }
      
   }

    if( $a == $b )
{
    echo " ".$a." ";
}
  }
  echo "<br><br>";
  echo $title2;
  echo "<br><br>";
  
  for( $c= 2; $c <= $values; $c++ )
{
   for( $d = 2; $d < $c; $d++ )
   {
     if( $c % $d == 0 )
      {
        break;
      }
      
   }

    if( $c != $d )
{
    echo " ".$c." ";
}
  }


  }

?>
</body>
</html>