PHP Program to check whether the number is odd or even


April 30, 2022, Learn eTutorial
1365

What are Odd and Even Numbers?

Odd numbers are those numbers Mod(%) 2 and the remainder will be non 0 and when the remainder will 0 then the number will be Even number. For example, if the entered number is 8 and when '8 % 2' is calculated and the result will be 0 so it is an even number, and if the entered number is 7 and when '7 % 2' is calculated the result will be 1 so it is an odd number.

How to check whether the number is odd or even using PHP

First, we have to accept the number from the user and assign it to a variable. Then we have to check the condition of inputted number Mod(%) 2 if the result is 0 then we can print the entered number as an even number or if the result of inputted number Mod(%) 2 is non 0 then the entered number can be printed as an odd number.

ALGORITHM

Step 1: Accept the number from the user and assign the number to the variable num

Step 2: Check the condition 'num % 2' if it is 0 then print the value of the variable num as an even number otherwise print the value of the variable num as the odd number

PHP Source Code

                                          <?php
$num = readline("Enter the number: ");
if ($num % 2 == 0) {
    echo "$num is Even Number";
} else {
    echo "$num is Odd Number";
}
?>
                                      

OUTPUT

Example 1
Enter the number: 86
86 is Even Number

Example 2
Enter the number: 77
77 is Odd Number