PHP script to find the odd or even numbers

Here, we find simple php script to find the given number is odd or even. These are the basic programs used during PHP training for students.

Simple program to find odd or even number

<?php
$num=$_REQUEST[‘num’];
if($num%2==0)
{
echo “Even number”;
}
else
{
echo “odd number”;
}
?>
<html>
<body>
<form action=”even.php” method=”post”>
Enter number<input type=”text” name=”num” />
<input type=”submit” name=”submit”/>
</form>
</body>
</html>

To find even or odd number using classes and objects

<?php
class even
{
public $num=null;
function processRequest()
{
$this->num=$_REQUEST[‘num’];
}

function run()
{
if(isset($_REQUEST[‘submit’]))
{
$this->processRequest();
if($this->num%2==0)
{
echo “Even Number”;
}
else
{
echo “Odd number”;
}
}
}
}
$objEven = new Even();
$objEven->run();

?>
<html>
<body>
<form action=”even.php” method=”post”>
Enter number<input type=”text” name=”num” />
<input type=”submit” name=”submit”/>
</form>
</body>
</html>

To find even or odd using simple function

<?php
$number=$_REQUEST[‘number’];

if( is_odd($number) )
{
echo “odd number”;
}
else
{
echo “even number”;
}
function is_odd( $int )
{
return( $int & 1 );
}
?>
<html>
<body>
<form action=”odd.php” method=”post”>
Enter number<input type=”text” name=”number” />
<input type=”submit” name=”submit”/>
</form>
</body>
</html>

Output

eve

 

 

 

Leave a Comment