Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript: How to check if a number is even without using the modulo operator?
For a given integer number, write a program in JavaScript to check whether a number is odd or even and return the same to the user. It's pretty easy to check whether a number is even by using the modulo operator. But, in this article, we will be checking whether a number is even or not without using the modulo operator.
Using the For Loop
In this approach, we are going to use the for loop to check whether a number is even or not. The idea is to take a Boolean flag variable as true and toggle it n times. After n iterations, if the flag returns to its original state (true), the number is even.
How It Works
We start with isEven = true. Each iteration flips the boolean value. After an even number of flips, we return to true; after an odd number, we get false.
// Returns true if n is even
function isEven(n) {
let flag = true;
for (let i = 1; i <= n; i++)
flag = !flag;
if (flag)
console.log(n + " is an Even number");
else
console.log(n + " is Odd");
}
// function calls
isEven(101);
isEven(158);
101 is Odd 158 is an Even number
Using Multiplication and Division
Here, we divide a number by 2, convert the result to an integer, then multiply by 2. If the final result equals the original number, it's even.
How It Works
For even numbers: 6 ÷ 2 = 3, 3 × 2 = 6 (matches original)
For odd numbers: 7 ÷ 2 = 3.5, parseInt(3.5) = 3, 3 × 2 = 6 (doesn't match 7)
// Returns true if n is even
function isEven(n) {
// Check if n/2 multiplied by 2 equals original number
if (parseInt(n / 2, 10) * 2 == n) {
console.log(n + " is an Even number");
} else {
console.log(n + " is Odd");
}
}
// function calls
isEven(101);
isEven(158);
101 is Odd 158 is an Even number
Using the Bitwise AND Operator
In this approach, we use the bitwise AND operator (&) to check if a number is even. We perform n & 1 - if the result is 0, the number is even; if 1, it's odd.
How It Works
In binary, even numbers end with 0, odd numbers end with 1. The bitwise AND with 1 isolates the last bit:
- Even:
6 & 1 = 110 & 001 = 000 = 0 - Odd:
7 & 1 = 111 & 001 = 001 = 1
// Returns true if n is even
function isEven(n) {
// n&1 is 0 for even, 1 for odd
if (!(n & 1)) {
console.log(n + " is an Even number");
} else {
console.log(n + " is Odd");
}
}
// function calls
isEven(101);
isEven(158);
101 is Odd 158 is an Even number
Comparison of Methods
| Method | Time Complexity | Efficiency | Readability |
|---|---|---|---|
| For Loop | O(n) | Low | Medium |
| Multiplication/Division | O(1) | High | High |
| Bitwise AND | O(1) | Highest | Medium |
Conclusion
The bitwise AND method is the most efficient approach for checking even numbers without modulo. For readability, the multiplication/division method is preferred, while the for loop approach is mainly educational.
