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!

PHP Program to find the even-odd number with form and database

PHP Program to find the even-odd number with form and database

In this tutorial, we will learn about the followings;

  1. PHP Program to find the even-odd number
  2. PHP Program to find the even-odd number with form 
  3. PHP Program to find the even-odd number with a database

PHP Program to find the even-odd number 

<?php
$a=4;
if($a%2==0)
{
echo $a ." is Even number";
}
else
{
echo $a . " is odd number";
}
echo "<title>Even | Odd</title>";

?>

PHP Program to find the even-odd number with form 

<html>
<body>
<form method="post" action="">
 <input type="text" name="num" placeholder="Enter Any Number">
 <input type="submit" name="sub" value="calculate">

</form>

</body>
</html>
 <?php
    if(isset($_POST['sub']))
	{
	  $NUM=$_POST['num'];
	  if($NUM%2==0)
	  {
	   echo "<h2>".$NUM. " is even number </h2>";
	  }
	  else 
	   {
	    echo "<h2>".$NUM. " is Odd number </h2>";
	   }
	}
 
 ?>

PHP Program to find the even-odd number with a database

<html>
<body>
<form method="post" action="">
 <input type="text" name="num">
 <input type="submit" name="sub" value="calculate">

</form>

</body>
</html>
 <?php
  include 'connection.php';
    if(isset($_POST['sub']))
	{
	  $NUM=$_POST['num'];
	  if($NUM%2==0)
	  {
	    mysql_query("insert into even(number) values('$NUM')");
		$sel=mysql_query("select * from even");
		
		  echo $NUM . " is  even";
		
		
	  }
	  else 
	   {
	     mysql_query("insert into odd(number) values('$NUM')");
		 $sel=mysql_query("select * from Odd");
		  echo $NUM. "   is odd";
	   }
	}
 
 ?>

now creates the connection.php file to make a database connection.

connection.php

<?php
	$conn = mysql_connect('localhost', 'root', '');
	if (!$conn)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("cal", $conn);
?>

Database to find the even-odd number Program

even odd php program with form and database

Figure: even odd PHP program with form and database.

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