Even Odd Program
Ex : Write a PHP program to print Even or odd number.
Even numbers are those which are divisible by 2. Numbers like 2,4,6,8,10, etc are even.
Odd numbers are those which are not divisible by 2. Numbers Like 1, 3, 5, 7, 9, 11, etc are odd.
Take a number.
Divide it by 2.
If the remainder is 0, print number is even.
Even Odd Program in PHP
A program to check 1233456 is odd or even is shown.
<?php
$number=1233456;
if($number%2==0)
{
echo "$number is Even Number";
}
else
{
echo "$number is Odd Number";
}
?>
Output: 1233456 is Even Number
By inserting value in a form we can check that inserted value is even or odd.
<html>
<body>
<form method="post">
Enter a number:
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if($_POST){
$number = $_POST["number"];
//divide entered number by 2
//if the reminder is 0 then the number is even otherwise the number is odd
if(($number % 2) == 0){
echo "$number is an Even number";
}else{
echo "$number is Odd number";
}
}
?>
Output:
On entering the number 23456, following output appears.
23456 is Even Number
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning