Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Next Prime Number Program in PHP with Form and Database

Next Prime Number Program in PHP with Form and Database

In this tutorial, we will learn about the following.

  1. Next Prime Number Program in PHP
  2. Next Prime Number Program in PHP with Form 
  3. Next Prime Number Program in PHP with  Database

Next Prime Number Program in PHP 

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<html>
<body>
<center>
<h2>Program 1</h2>
<p>Next Prime number program in PHP </p>

<?php

 $NextPrime=0;
 $Counter=2;
 $num=17;
  for($NextPrime=$num+1;$NextPrime<10000;$NextPrime++)
   {
    for($Counter=2;$Counter<$NextPrime;$Counter++)
      {
     if($NextPrime %$Counter==0)
     {
     break;
      } 
     } 
     if($NextPrime==$Counter || $NextPrime==1)
       {
       echo"You entered $num and next prime is $NextPrime";
    break;
    } 
    }  

?>
</center>
</body>
</html>

Next Prime Number Program in PHP with Form 

In this example, we will take the inputs from the user and store them in variables.

<html>
<body>
<center>
<h2>Program 2</h2>
<p>Next Prime number program in PHP (Using Form )</p>
<h2>
<form method="post">
<label>Enter any number</label><br />
<input type="text" name="number"  /><br />
<input type="submit" name="submit" />
<br /><br />
</form></h2>

<?php

 $NextPrime=0;
 $Counter=2;
 $num=$_POST['number'];
  for($NextPrime=$num+1;$NextPrime<10000;$NextPrime++)
   {
    for($Counter=2;$Counter<$NextPrime;$Counter++)
      {
     if($NextPrime %$Counter==0)
     {
     break;
      }
     } 
     if($NextPrime==$Counter || $NextPrime==1)
       {
       echo"You entered $num and next prime is $NextPrime";
    break;
    } 
    }  

?>
</center>
</body>
</html>

Next Prime Number Program in PHP with Database

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>

<body>
<center>
<h2>Program 3</h2>
<p>Next Prime number program in PHP (Using Form with SQL)</p>
<h2>
<form method="post">
<label>Enter any number</label><br />
<input type="text" name="number"  /><br />
<input type="submit" name="submit" />
<br /><br />
</form></h2>

<?php

$con=mysqli_connect("localhost","root","","test2");
 if(isset($_POST['submit'])) 
   {
 $NextPrime=0;
 $Counter=2;
 $num=$_POST['number'];
  for($NextPrime=$num+1;$NextPrime<10000;$NextPrime++)
   {
    for($Counter=2;$Counter<$NextPrime;$Counter++)
      {
     if($NextPrime %$Counter==0)
     {
     break;
      }
     } 
     if($NextPrime==$Counter || $NextPrime==1)
       {
       echo"You entered $num and next prime is $NextPrime";
    break;
    } 
    }  

$sql=mysqli_query($con,"INSERT INTO nextprime (Input,NextPrime) VALUES ($num,$NextPrime)");
   }
?>
</center>
</body>
</html>

Database of Next Prime Number Program

next prime program in php woth database and form

Figure: next prime program in PHP with database and form

 

Prof. Fzal Rehman Shamil
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325