Saturday, July 13, 2024
HomeLanguagesJavascriptJavaScript Program to Check if a Number is Odd or Even

JavaScript Program to Check if a Number is Odd or Even

In this article, 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.

In other words, Those numbers which are completely divisible by 2 (give the remainder 0 after dividing by 2) are known as even numbers and those which divided by 2 and leave a reminder 1 are called an odd number.

Even = {2k : k ∈ Z}
Odd = {2k + 1 : k ∈  Z}
where k is an integer. 

Example:

Input: 2
Output: Even number

Input: 41
Output: Odd Number

 

There are several methods that can be used to Check if a Number is Odd or Even, which are listed below:

  • Using modulo operator
  • using Bitwise & Operator
  • Using Bitwise OR Operator (|)
  • Using Ternary Operator

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using the modulo Operator in JavaScript

In this method, we use the JavaScript Modulo Operator (%) to check whether the number is even or odd in JavaScript. We will evaluate the N % 2, and if the result is 0 then the number is even otherwise the number is odd.

Syntax:

remainder = var1 % var2

Example: In this example, we are using the modulo operator to find given number is even or odd.

Javascript




// Returns true if n is
// even, else odd
function isEven(n) {
    return (n % 2 == 0);
}
  
// Driver code
let n = 101;
  
isEven(n) ? console.log("Even") : console.log("Odd");


Output

Odd

Approach 2: Using Bitwise & Operator

A better solution is to use bitwise operators. We need to check whether the last bit is 1 or not. If the last bit is 1 then the number is odd, otherwise the number is even.

Syntax:

a & b

Example: In this example, we are using the bitwise & operator to find given number is even or odd.

Javascript




function checkOddOrEven(n) {
    if (n & 1 == 1) {
        return "Number is odd";
    }
    return "Number is even";
}
  
console.log(checkOddOrEven(12));
console.log(checkOddOrEven(121));


Output

Number is even
Number is odd

Approach 3: Using Bitwise OR Operator (|)

JavaScript Bitwise OR(|) Operator is used to compare two operands by performing OR operation on individual bits of the operands and returns true even if one of the compared bits is 1.

Syntax:

a | b

Example: In this example, we are using the bitwise OR operator to find whether our given number is even or odd.

Javascript




function checkOddOrEven(number) {
    return (number | 1) === number ? 'Odd' : 'Even';
}
  
console.log(checkOddOrEven(14));
console.log(checkOddOrEven(17));


Output

Even
Odd

Approach 4: Using Ternary Operator

Ternary operator (?:) is a shorthand conditional operator that evaluates a condition and returns one of two expressions based on the result.

Syntax:

condition ? value if true : value if false

Example: In this example, we are using the Ternary operator to find given number is even or odd.

Javascript




function checkOddOrEven(num) {
    return num % 2 === 0 ? 'Even' : 'Odd';
}
  
console.log(checkOddOrEven(21));
console.log(checkOddOrEven(12));


Output

Odd
Even

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments