Prime Number Program in PHP with Form and Database
By: Prof. Dr. Fazal Rehman | Last updated: March 3, 2022
Prime Number Program in PHP with Form and Database.
In this tutorial, we will learn about the following;
Prime Number Program in PHP
Prime Number Program in PHP with Form
Prime Number Program in PHP with Database
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.
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<html>
<body>
<center>
<h2>Program1</h2>
<p>PrimenumberprograminPHP</p>
<?php
$num=29;
$LeastPrime=2;
if($num<2)
{
echo"$num is less than 2 Smallest prime number is 2";
}
for($LeastPrime=2;$LeastPrime<$num;$LeastPrime++)
{
if($num%$LeastPrime==0)
{
echo"$num is not a prime number";
break;
}
}
if($num==$LeastPrime)
{
echo"$num is a Prime number";
}
?>
</center>
</body>
</html>
Prime Number Program in PHP with Form.
In this example, we will take the inputs from the user and store them in variables.
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<html>
<body>
<center>
<h2>Program2</h2>
<p>Prime number program inPHP(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
if(isset($_POST['submit'])){
$num=$_POST['number'];}
$Counter=2;
if($num<2)
{
echo"$num is less than 2 and Smallest prime number is 2";
}
for($Counter=2;$Counter<$num;$Counter++)
{
if($num%$Counter==0)
{
echo"$num is not a prime number";
break;
}
}
if($num==$Counter)
{
echo"$num is a Prime number";
}
?>
</center>
</body>
</html>
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.
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<html>
<body>
<center>
<h2>Program3</h2>
<p>Prime number program inPHP(Using Form andSQL)</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
if(isset($_POST['submit'])){
$num=$_POST['number'];
$Counter=2;
if($num<2)
{
$result="$num is less than 2 and Smallest prime number is 2";
echo"$num is less than 2 and Smallest prime number is 2";