Here I’ll show you how to check whether a number is odd or even using PHP, Python, and JavaScript.
Programming logic to check whether a number is odd or even
theNumber % 2 == 0
If a number is divided by 2 and the remainder is 0, then the number is an even number otherwise the number is an odd number.
Modulo operator (%)
In most of the programming language the modulo operator (%
) is used to compute the remainder.
After getting the remainder, we will check whether the remainder is equal to 0 or not, If yes, then it is an even number, otherwise it is an odd number.
- PHP
- JavaScript
- Python
<?php
$num = 6;
if ($num % 2 === 0) {
echo "$num is Even";
} else {
echo "$num is Odd";
}
const num = 6;
if (num % 2 === 0) {
console.log(num + " is Even");
} else {
console.log(num + " is Odd");
num = 6
if(num % 2 == 0):
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))