Javascript if odd number

Javascript Program to Check if a Number is Odd or Even

To understand this example, you should have the knowledge of the following JavaScript programming topics:

Even numbers are those numbers that are exactly divisible by 2.

The remainder operator % gives the remainder when used with a number. For example,

const number = 6; const result = number % 4; // 2 

Hence, when % is used with 2, the number is even if the remainder is zero. Otherwise, the number is odd.

Example 1: Using if. else

// program to check if the number is even or odd // take input from the user const number = prompt("Enter a number: "); //check if the number is even if(number % 2 == 0) < console.log("The number is even."); >// if the number is odd else
Enter a number: 27 The number is odd.

In the above program, number % 2 == 0 checks whether the number is even. If the remainder is 0, the number is even.

Читайте также:  Python сортировка строки чисел

In this case, 27 % 2 equals to 1. Hence, the number is odd.

The above program can also be written using a ternary operator.

Example 2: Using Ternary Operator

// program to check if the number is even or odd // take input from the user const number = prompt("Enter a number: "); // ternary operator const result = (number % 2 == 0) ? "even" : "odd"; // display the result console.log(`The number is $.`);
Enter a number: 5 The number is odd. 

Источник

How to check if a number is odd or even in JavaScript

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

In JavaScript, determining whether a number is even or odd is a simple task that can be accomplished with a few lines of code. In this Answer, we will explore the different methods available for checking whether a number is even or odd in JavaScript.

Method 1: Using the modulus operator (%)

The modulus operator (%) is used to obtain the remainder when one number is divided by another. This operator can be used to determine whether a number is odd or even by determining whether the remainder after dividing it by two equals zero or not. It is even if it equals 0. Otherwise, it is odd.

//check if the number is even
function isEven (number)
return number % 2 === 0;
>
//check if the number is odd
function isOdd (number)
return number % 2 !== 0;
>
//test
console.log(isEven(5)); //false
console.log(isEven(4)); //true
console.log(isOdd(5)); //true
console.log(isOdd(4)); //false

In the code above, the isEven() function checks whether a number is even by using the modulus operator to check whether the remainder of dividing the number by 2 is equal to 0 (zero).

The isOdd() function checks whether a number is odd by using the modulus operator to check whether the remainder of dividing the number by 2 is not equal to 0 (zero).

Method 2: Using bitwise AND operator (&)

Another way to determine whether a number is even or odd is by using the bitwise AND operator (&). When this operator is applied to a number and 1 , the result is 0 if the number is even and 1 if the number is odd.

//check if the number is even
function isEven (number)
return (number & 1) === 0;
>
//check if the number is odd
function isOdd (number)
return (number & 1) === 1;
>
//Test
console.log(isEven(5)); //false
console.log(isEven(4)); //true
console.log(isOdd(5)); //true
console.log(isOdd(4)); //false

In the code above, the isEven() function uses the bitwise AND operator to check whether the last bit of the number is 0, which indicates that the number is even.

The isOdd() function uses the bitwise AND operator to check whether the last bit of the number is 1, which indicates that the number is odd.

Method 3: Using Math.floor() and division(/)

A third way to determine whether a number is even or odd is by using the Math.floor() function and division ( / ). If we divide a number by 2 and then take the floor of the result, we get the integer part of the result. If the integer part is the same as the original number, then the number is even. Otherwise, it is odd.

Источник

Check if a number is odd or even in JavaScript

There is no in-built function or method to check if a number is odd or even in JavaScript. We therefore have to create a function ourselves to perform this check.

This can be done with the help of the remainder operator ( % ).

Since an even number always has a remainder of zero, if applying the remainder operator to a number returns this outcome, the number must be even. Conversely, if a remainder exists, the number must be odd.

Below is a function that perform this check, including a pre-check for whether the number passed into it when it is called is an integer. The function is then called three times:

function oddOrEven(x) < // First, check if number is integer. If not, // keyword return stops function processing // and a console error is created if (!Number.isInteger(x)) < return console.error("Not an integer!") >// Checks the remainder of the number passed // into the function if divided by 2. If it // is 0, the if statement is executed, if it // is not, the else statement runs. if (x % 2 === 0) < console.log("The number is even"); >else < console.log("The number is odd"); >> oddOrEven(2) // The number is even oddOrEven(3) // The number is odd oddOrEven(0.2) // Not an integer

Alternative solution using the ternary operator

The odd or even number check function can also be written using the ternary operator. The syntax is as follows: statement : returnIfTrue : returnIfFalse .

Note that because a ternary statement requires a returnIfFalse expression, it is only applied to the check of whether a number is even or odd:

function oddOrEven(x) < if (!Number.isInteger(x)) < return console.error("Not an integer") >x % 2 === 0 ? console.log("The number is even") : console.log("The number is odd") > oddOrEven(2) // The number is even oddOrEven(3) // The number is odd oddOrEven(0.2) // Not an integer

Источник

How to determine if a number is odd or even in JavaScript?

In this tutorial, let’s see how to determine whether a number is odd or even using JavaScript. The first step is understanding the logic. What are even numbers? Even numbers will be exactly divisible by 2. The remainder will be zero. What are odd numbers? Odd numbers will carry a remainder when divided by 2.

Using the if-else Condition and Modulo Operator

«if» will check a condition, and the if block code will be executed if the condition is true. If the condition is false, else block code will be executed.

The modulo operator in JavaScript returns the remainder.

Syntax

Users can follow the below syntax to use the if else statement and modulus operator.

Algorithm

  • STEP 1 − Read the number to variable n.
  • STEP 2 − Divide n by 2 and save the remainder in a variable named r.
  • STEP 3
If r==0, print “even” If r!=0, print “odd”.

Example

In this example, we have assigned 156 to a variable. Next, we apply the syntax given above. Here, if block checks for the remainder and it is 0 here. Hence the if block gets executed and we get the output as «156 is Even.

html> body> h2>i>if else/i> and i>modulus operator/i> to find odd or even/h2> div id="number">/div> script> let numoutput=document.getElementById("number"); var nmbr = 156; if(nmbr % 2 == 0) numoutput.innerHTML = nmbr + " is Even"; else numoutput.innerHTML = nmbr + " is Odd"; /script> /body> /html>

Using the Ternary and Modulo Operators

A ternary operator is an operator with three operands. The condition, a value if the condition is true, and another value if the condition is false are the three operands of the expression.

If the condition is true, the value between «?» and «:» is executed. Similarly, if the condition returns false, the value that follows «:» is performed.

Syntax

Users can follow the below syntax to use the ternary operator.

var result = condition? "true block": "false block";

Example

In this example, 131 is assigned to a variable. Next, we check for its remainder using the syntax above. Here the remainder is 1. So, the false block of the ternary operation gets executed. Hence, we get the output»number is odd».

html> body> h2>Using i>Ternary Operator/i> and i>modulus operator/i>/h2> div id="number">/div> script> let output=document.getElementById("number"); var nbr = 131; (nbr % 2 == 0) ?output.innerHTML = nbr +" is even" : output.innerHTML= nbr+ " number is odd"; /script> /body> /html>

Using Bitwise XOR Operator

When a number is bitwise XORed by 1, the number gets incremented by 1, if it is even and gets decremented by 1, if it is odd.

Consider 11 XOR 1 1011 0001 ----- 1010 is 10, and it's a decrement; hence 11 is an odd number.

Users can follow the below syntax to use the bitwise xor operator.

Syntax

if ((n ^ 1) == (n + 1))//bitwise xor logic //even else //odd

Example

In this example, we take the number 100. Using the ternary operation, we pass it to an odd-even check function where the bitwise XOR logic is checked and a boolean value is returned. If it is true, we display that the number is «even» or else the number is «odd».

html> body> h2>Using i>bitwise xor operator/i>/h2> div id="number">/div> script> function isOddEven(n) if ((n ^ 1) == (n + 1))//even return true; else //odd return false; > let output=document.getElementById("number"); var num = 100; isOddEven(num) ? output.innerHTML = num + " Even" : output.innerHTML = num + " Odd" ; /script> /body> /html>

Using Bitwise AND Operator

When a number undergoes AND with 1 and if the last bit of the result is 1, it is an odd number. If the last bit is zero, it is an even number.

Take the binary value of numbers 10 and 1. Then AND both. 0000 1010 0000 0001 -------------- 0000 0000 Here the output bits are zero and hence 10 is an even number.

Syntax

Users can follow the below syntax to use bitwise and operator.

if(number & 1)//bitwise and logic //odd else //even

Example

In this example, first, we pass 93 to the check function. The function performs bitwise AND operation, as we have seen above. If 93 & 1 returns 1, it’s considered odd and else considered even. This boolean value is then used to display the output «93 odd» using the document. write method.

html> body> h2>Using i>bitwise and operator/i>/h2> div id="number">/div> script> let output=document.getElementById("number"); function check(number) var isOdd = number & 1; var isEven= !(number & 1); if(isOdd) output.innerHTML= "
"
+ number + " odd"; > else output.innerHTML = "
"
+ number + " even"; > check(46); /script> /body> /html>

Using Bitwise OR Operator

When a number undergoes bitwise OR with 1 and if the result is the same number, it is an odd number. If the number gets incremented by 1, it is an even number.

Take 1011 OR 1 1011 0001 ------ 1011 Here the output bits are equal to 11 hence 11 is an odd number.

Syntax

Users can follow the below syntax to use bitwise or operator.

if ((n | 1) > n)//bitwise or logic //even else //odd

Example

In this example, we take the number 100 and pass it to the check function. Here the number undergoes bitwise OR logic as explained above. If the result is greater than the number, it is even. Else it is odd. Here bitwise OR result of 100 is greater than 100 and hence 100 is an even number. From the boolean value returned by the check function, we display the output using the document. write method.

html> body> h2>Using i>bitwise or operator/i>/h2> div id="number">/div> script> function checkOddEven(n) if ((n | 1) > n)//even return true; else //odd return false; > let output=document.getElementById("number"); var n = 100; output.innerHTML = checkOddEven(n) == true ? n + " is Even": n + " is Odd"; /script> /body> /html>

Источник

Оцените статью