PHP check if a number is Even or Odd
Last Updated :
11 Sep, 2024
Checking if a number is even or odd involves determining whether the number is divisible by 2. An even number has no remainder when divided by 2, while an odd number has a remainder of 1. This can be done using various methods like modulo or bit manipulation.
Examples :
Input : 42
Output : Even
Explanation: The number 42 is divisible by 2
Input : 39
Output : Odd
Explanation: The number 39 is not divisible by 2
We can solve this problem in two different ways as described below:
Using modulo (%) operator
The modulo (%) operator checks if a number is even or odd by dividing it by 2 and examining the remainder. If the remainder is 0, the number is even; if the remainder is not 0 (1), the number is odd.
Example: In this example we defines a function check() to determine if a number is even or odd. It checks the remainder when divided by 2, and prints "Even" or "Odd."
PHP
</p><pre><code class="language-php"><?php
// PHP code to check whether the number
// is Even or Odd in Normal way
function check($number){
if($number % 2 == 0){
echo "Even";
}
else{
echo "Odd";
}
}
// Driver Code
$number = 39;
check($number)
?></code></pre><div class="code-output"><br><strong>Output</strong><pre>Odd
</pre></div><span>
</span><b><strong>Time Complexity</strong></b><span>: O(1) </span><p></p><h2 id="recursive-method"><b><strong>Recursive method</strong></b></h2><p dir="ltr"><span>The recursive approach checks if a number is even or odd by repeatedly subtracting 2 from the number in each recursive call. If the number reaches 0, it's even; if it reaches 1, it's odd. </span></p><p dir="ltr"><b><strong>Example:</strong></b><span> In this example we use recursive function check() to determine if a number is even or odd by subtracting 2 until it reaches 0 (even) or 1 (odd), handling negative numbers as well.</span></p><p dir="ltr"><gfg-tabs data-run-ide="true" data-mode="light"><gfg-tab slot="tab">PHP
<?php
// Recursive function to check whether
// the number is Even or Odd
function check($number){
if($number == 0)
return 1;
else if($number == 1)
return 0;
else if($number<0)
return check(-$number);
else
return check($number-2);
}
// Driver Code
$number = 39;
if(check($number))
echo "Even";
else
echo "Odd";
?>
Time Complexity: O(n) Using Bit Manipulation:
Using bit manipulation, you can check if a number is even or odd in PHP by examining the least significant bit. If number & 1 equals 0, the number is even; if it equals 1, the number is odd.
Example: In this example we checks if a number is even or odd using bitwise AND (&). It compares the number's least significant bit with 1; if true, it's odd; otherwise, it's even.PHP
<?php
// PHP code to check whether the number
// is Even or Odd using Bitwise Operator
function check($number)
{
// One
$one = 1;
// Bitwise AND
$bitwiseAnd = $number & $one;
if($bitwiseAnd == 1)
{
echo "Odd";
}
else{
echo "Even";
}
}
// Driver Code
$number = 39;
check($number)
?>
Output :Odd
Time Complexity: O(1) PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
JavaScript Program to Check if a Number is Odd or Even We are going to learn how to check whether the number is even or Odd using JavaScript. In the number system, any natural number that can be expressed in the form of (2n + 1) is called an odd number, and if the number can be expressed in the form of 2n is called an even number.Even = {2k : k â Z}(whe
3 min read
Check whether a given number is even or odd in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Examples: Input: 2 Output: even Input: 5 Output: odd Th
1 min read
PHP | Check if a number is Perfect number A perfect number is a number if it is equal to the sum of its factors,that is original number is equal to sum of all its factors excluding the number itself. We have already discuss how to check if a number is perfect or not in this article. In this article we will discuss about how to do the same i
2 min read
Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
PHP | Check if a number is armstrong number Given a number, we need to check whether it is an armstrong number or not in PHP. An Armstrong number is the one whose value is equal to the sum of the cubes of its digits.Examples: Input : 407 Output : Yes 407 = (4*4*4) + (0*0*0) + (7*7*7) = 64 + 0 + 343 = 407 Input : 303 Output : No Approach: For
2 min read
Check Whether a Given Number is Ugly Number or Not in PHP Given an integer N, the task is to find out whether the given number is an Ugly number or not. Ugly numbers are numbers whose only prime factors are 2, 3 or 5.Examples: Input: N = 14 Output: No Explanation: 14 is not ugly since it includes another prime factor 7.Input: N = 6 Output: Yes Explanation:
3 min read