I came across http://rindovincent.blogspot.com/p/javascript.html where there was a simple Javascript program to find whether the number is odd or even. I am pasting the same code with permission here.

var n = prompt(“Enter a number to find odd or even”, “Type your number here”);
n = parseInt(n);
if (isNaN(n))
{
alert(“Please Enter a Number”);
}
else if (n == 0)
{
alert(“The number is zero”);
}
else if (n%2)
{
alert(“The number is odd”);
}
else
{
alert(“The number is even”);
}

It’s only 10 years later, but I now ’stumbled‘ upon this…

Yes Pj, this is the best and most elegant.

As marwils already wrote, it’s a bitwise AND operation.
This means that you only check whether the most right (*) bit of the most right byte of the number (**) is set. If so, it is odd, otherwise it is even.

I have the feeling that most today’s programmers don’t know the concept of a ’bit‘, they’re only dealing with objects and inheritance. That’s OK, no doubt, but let‘s not forget the basics, the bits :-)

(*) Most right, most left, first, last: It all depends how you visualize a byte and/or how it is implemented into the processor.
(**) The same goes for numbers > 255, they need more than 1 byte.

I’ve read that the == comparison is dangerous because it does type coercion to make comparisons so it’s better practice to use === which will return true if both the left/right side are truly identical.

Generally, it’s best practice to use === but in this example, value % 2 is guaranteed to return an integer or NaN, which allows us to absolutely know the type of variable we’re comparing on either side of num % 2 or 0, but not num itself.

The problem comes down to if you accidentally get a false or true as an input, which loosely converts to a 0 or 1 when using the % operator. This might cause some problems in your code if false returns true. You normally would use a equality (===) operator to make this comparison, but since the boolean gets converted to a number on the % operator, equality won’t work here as most have typed the function.

Given the unknown nature of the use for this function and given it’s name, I’d say the following would be safe to use:

function isEven(num) {
return isNaN(num) && num !== false && num !== true ? false : num % 2 == 0;
}

or for those who enjoy readable code:

function isEven(num) {
if(num !== false && num !== true && !isNaN(num)) {
return num % 2 == 0;
} else return false;
}

also not to mention == is faster than ===, so if you know what you are doing it’s faster to use
the == when applicable but if you are not sure, use ===

There are actually numerous ways to do this other than the two you have listed. Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:

if (x % 2 == 0)
total += 1; //even number
else
total -= 1; //odd number

Definitely worth a read for those that are curious.

The three equal signs === is a “strict” comparison operator. It evaluates whether the contents on both sides are the same. If they are, it returns true; if not, then false. In this case, 2 is not the same as 3, so it will return false.

Why not just

function isEven(value) {

return (value%2 == 0);

}

printing it whether it z odd or even is equally important…!!! but the logic is quite good.. sweet and simple..!!

And the PHP version only needs an extra dollar sign:

function isEven($int){

return ($int%2 == 0);

}

In these times, I can assume most webdevvers use a javascript library like jQuery or similar. These libraries have functions like isEven built in.

For those who don’t use such libraries: great snippet ;)

I came across http://rindovincent.blogspot.com/p/javascript.html where there was a simple Javascript program to find whether the number is odd or even. I am pasting the same code with permission here.

var n = prompt(“Enter a number to find odd or even”, “Type your number here”);

n = parseInt(n);

if (isNaN(n))

{

alert(“Please Enter a Number”);

}

else if (n == 0)

{

alert(“The number is zero”);

}

else if (n%2)

{

alert(“The number is odd”);

}

else

{

alert(“The number is even”);

}

Thaannkkkk yoouuu soooo muccchhh..very helpful

:D

good syntax

function isEven(value)

{

return /^\d+$/.test(value.toString()) ? (value%2 === 0 ? true : false ) : false;

};

//so, my turn =)

function isEven(value){

return isFinite(+value) && !(+value % 2)

}

function isOdd(n){

return 1&n;

}

Do i win?

yes, you win

That is the good one. But what means 1&n ? Can you please explain?

It’s a bitwise AND operation.

See: https://en.wikipedia.org/wiki/Bitwise_operation#AND

The result of 1&n is always 1, if n is a odd number.

It’s only 10 years later, but I now ’stumbled‘ upon this…

Yes Pj, this is the best and most elegant.

As marwils already wrote, it’s a bitwise AND operation.

This means that you only check whether the most right (*) bit of the most right byte of the number (**) is set. If so, it is odd, otherwise it is even.

I have the feeling that most today’s programmers don’t know the concept of a ’bit‘, they’re only dealing with objects and inheritance. That’s OK, no doubt, but let‘s not forget the basics, the bits :-)

(*) Most right, most left, first, last: It all depends how you visualize a byte and/or how it is implemented into the processor.

(**) The same goes for numbers > 255, they need more than 1 byte.

start

read n

reminder=n/2

if (reminder=0)

print”number is even”

else

print”number is odd”

stop

I’ve read that the == comparison is dangerous because it does type coercion to make comparisons so it’s better practice to use === which will return true if both the left/right side are truly identical.

Generally, it’s best practice to use

`===`

but in this example,`value % 2`

is guaranteed to return an integer or`NaN`

, which allows us to absolutely know the type of variable we’re comparing on either side of`num % 2`

or`0`

, but not`num`

itself.The problem comes down to if you accidentally get a

`false`

or`true`

as an input, which loosely converts to a`0`

or`1`

when using the`%`

operator. This might cause some problems in your code if`false`

returns`true`

. You normally would use a equality (`===`

) operator to make this comparison, but since the boolean gets converted to a number on the`%`

operator, equality won’t work here as most have typed the function.Given the unknown nature of the use for this function and given it’s name, I’d say the following would be safe to use:

or for those who enjoy readable code:

@Evan hit it right on the head

also not to mention == is faster than ===, so if you know what you are doing it’s faster to use

the == when applicable but if you are not sure, use ===

Cheers

include<stdio.h>

include<conio.h>

main()

{

int n;

clrscr();

printf(“Enter a Number:\n”);

scanf(“%d”,&n);

if(n%2==0)

{

printf(“Even Number”);

}

else

{

printf(“Odd Number”);

}

getch();

}

Hey

I was just wondering

What is the equation “(n % 2 == 0)” actually doing?

Odd And Even syntax

Odd Syntax is:

Having the Even function…

And what if the “value” isn’t a base 10 number?

var a;

var b=”ODD”;

var c=”EVEN”;

a=parseInt(window.prompt(“Enter The Value:”));

document.write(“Your Value is: “,chk(a));

function chk(a)

{

if (a%2==0)

return c;

else

return b;

}

function isEven(value) {

return !x%2;

}

Here’s a blog article which benchmarks quite a few ways to test if a number is odd or even:

http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-check-if-a-number-is-odd-or-even

There are actually numerous ways to do this other than the two you have listed. Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:

Definitely worth a read for those that are curious.

How can I write a function, that takes in a number and returns true or false if the value is both greater than zero and odd ?

Or we can use the bitwise AND operator.

function oddOrEven(x) {

return ( x & 1 ) ? “odd” : “even”;

}

Sort array elements, with even numbers first followed by odd numbers. (document.write and input the value with prompt)(Javascript) plsss help me..

I use modulo a lot… in BED, for example, to log progress:

if (0 === processed % 1000) {

sprintf(“%s records processed”, processed)

}

7 easy ways to find whether a number id odd/even.

http://www.waytosmart.com/tutorials/c-programming/c-program-to-check-odd-or-even-number-easy-ways.php

where to input these codes and where to view that a number is odd ..please guys tell me

Hi @mahnoor,

This snippet would go into any JavaScript file that you are serving on the site.

Cheers!

Or you could do something like this….

Just a quick followup: You may also need something like Babel (https://babeljs.io/), if your system doesn’t support ES6. Thank! A

You could even shorten this more by doing something like:

// change the value of

`number`

to test your Code//If number equals 1, then odd should be printed to the console.

//If number equals 2, then Even should be printed to the console.

var number = 2;

if (number%2===1) {

console.log(“odd”)

} else{

console.log(“Even”);

}

could anyone explain what 2===0 mean or how it work

The three equal signs

`===`

is a “strict” comparison operator. It evaluates whether the contents on both sides are the same. If they are, it returns true; if not, then false. In this case, 2 is not the same as 3, so it will return false.