JavaScript Program to Count Even and Odd Numbers in an Array
Last Updated :
31 Aug, 2023
In this article, we will write a program to count Even and Odd numbers in an array in JavaScript. Even numbers are those numbers that can be written in the form of 2n, while Odd numbers are those numbers that can be written in the form of 2n+1 form.
For finding out the Even and Odd numbers in an array there are various methods:
- Using Modulo Operator
- using Bitwise & Operator
- Using Bitwise OR Operator (|)
- Using Ternary Operator
Approach 1: Using the Modulo Operator in JavaScript
The Modulo Operator is used to get the remainder, The number will be an even number if it gives zero after taking the modulo with 2 else it will be an odd number.
Example: It describes how we can count the number of even and odd numbers present in an array using the modulo operator
JavaScript
// Creating an Array
let array = [1, 2, 3, 4, 5, 6];
// Count of odd number and
// even number
// will be zero initially
let oddNum = 0;
let evenNum = 0;
// For loop for counting
// odd and even number
for (let index = 0; index < array.length; index++) {
if (array[index] % 2 == 0) {
evenNum++;
}
else {
oddNum++;
}
}
// Printing the result
console.log("Total even number: " + evenNum);
console.log("Total odd number: " + oddNum);
OutputTotal even number: 3
Total odd number: 3
It is an another approach via we can find out the number is odd or even. It chceks for the binary number of the given number if the last bit of that binary number is 1 then it will be odd else it will be even.
Example: It describes how we can count the number of even and odd numbers present in an array using Bitwise & Operator
JavaScript
// Creating an array
let array = [1, 2, 3, 4, 5, 6, 7];
// Initial count should be 0
let oddNum = 0;
let evenNum = 0;
// For loop for counting
// even odd numbers
for (let index = 0; index < array.length; index++) {
if (array[index] & 1 == 1) {
oddNum++;
}
else {
evenNum++;
}
}
// Printing the total number of
// even and odd numbers
console.log("Total even number: " + evenNum);
console.log("Total odd number: " + oddNum);
OutputTotal even number: 3
Total odd number: 4
Approach 3: Using Bitwise OR Operator (|)
This approach is using bitwise or operator. In this we perform the or operation with given number and if result gets the exact same number then the number will be a odd number else it will be even number.
Example: It describes how we can count the number of even and odd numbers present in an array using Bitwise OR Operator
JavaScript
// Creating an array
let array = [1, 2, 3, 4, 5, 6, 7];
// Initial count will be 0
let oddNum = 0;
let evenNum = 0;
// For loop for counting
// even and odd numbers
for (let index = 0; index < array.length; index++) {
if ((array[index] | 1) === array[index]) {
oddNum++;
} else {
evenNum++
}
}
// Printing the result
console.log("Total even number: " + evenNum);
console.log("Total odd number: " + oddNum);
OutputTotal even number: 3
Total odd number: 4
Approach 4: Using Ternary Operator
In this approach we are using ternary operator which checks, If result is 0 after performing modulo 2 with number then it will be an even number else it will be an odd number.
Example: It describes how we can count the number of even and odd numbers present in an array using ternary operator
JavaScript
// Creating an array
let array = [1, 2, 3, 4, 5, 6, 7];
// Initial count will be 0
let oddNum = 0;
let evenNum = 0;
// For loop for counting
// even and odd numbers
for (let index = 0; index < array.length; index++) {
// Use of ternary operator
(array[index] % 2 == 0) ? evenNum++ : oddNum++
}
// Printing the result
console.log("Total even number: " + evenNum);
console.log("Total odd number: " + oddNum);
OutputTotal even number: 3
Total odd number: 4
Similar Reads
JavaScript Program to Find Sum of Even Numbers of an Array In JavaScript, working with arrays is a basic operation. We have to sum all the even numbers present in the array. We can check the number if it is even or not by the use of the % operator. These are the following ways to find the sum of Even numbers in an Array: Table of Content Iterative ApproachU
4 min read
JavaScript Program to Find Sum of Odd Numbers in an Array In JavaScript, working with arrays is a common task. Often one needs to manipulate the elements within an array, such as finding specific values or calculating their sum. Below are the approaches to find the sum of all odd numbers within an array: Table of Content 1. Using a loop2. Using the filter
4 min read
Javascript Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix.Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }Output : Frequency of odd number = 5 Frequency of even number = 4Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18
3 min read
JavaScript program to print even numbers in an array Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter
5 min read
Print all Even Numbers in a Range in JavaScript Array We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read