Testing Whether a Value Is Odd or Even

Testing whether a value is odd or even

Use modulus:

function isEven(n) {
return n % 2 == 0;
}

function isOdd(n) {
return Math.abs(n % 2) == 1;
}

You can check that any value in Javascript can be coerced to a number with:

Number.isFinite(parseFloat(n))

This check should preferably be done outside the isEven and isOdd functions, so you don't have to duplicate error handling in both functions.

How do I check if an integer is even or odd?

Use the modulo (%) operator to check if there's a remainder when dividing by 2:

if (x % 2) { /* x is odd */ }

A few people have criticized my answer above stating that using x & 1 is "faster" or "more efficient". I do not believe this to be the case.

Out of curiosity, I created two trivial test case programs:

/* modulo.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x % 2)
printf("%d is odd\n", x);
return 0;
}

/* and.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x & 1)
printf("%d is odd\n", x);
return 0;
}

I then compiled these with gcc 4.1.3 on one of my machines 5 different times:

  • With no optimization flags.
  • With -O
  • With -Os
  • With -O2
  • With -O3

I examined the assembly output of each compile (using gcc -S) and found that in each case, the output for and.c and modulo.c were identical (they both used the andl $1, %eax instruction). I doubt this is a "new" feature, and I suspect it dates back to ancient versions. I also doubt any modern (made in the past 20 years) non-arcane compiler, commercial or open source, lacks such optimization. I would test on other compilers, but I don't have any available at the moment.

If anyone else would care to test other compilers and/or platform targets, and gets a different result, I'd be very interested to know.

Finally, the modulo version is guaranteed by the standard to work whether the integer is positive, negative or zero, regardless of the implementation's representation of signed integers. The bitwise-and version is not. Yes, I realise two's complement is somewhat ubiquitous, so this is not really an issue.

How to determine if a number is odd in JavaScript

Use the below code:

function isOdd(num) { return num % 2;}console.log("1 is " + isOdd(1));console.log("2 is " + isOdd(2));console.log("3 is " + isOdd(3));console.log("4 is " + isOdd(4));

Test if number is odd or even

You were right in thinking mod was a good place to start. Here is an expression which will return true if $number is even, false if odd:

$number % 2 == 0

Works for every integerPHP value, see as well Arithmetic OperatorsPHP.

Example:

$number = 20;
if ($number % 2 == 0) {
print "It's even";
}

Output:

It's even

How to determine if a number is odd or even in Java script

Use the modulus operator

if(intellect % 2 == 0)
{
alert ('is even');
}
else
{
alert('is odd');
}

Testing if a high number is odd or even in C

The problem is not in the modulo operation, but in the type of data you are using.

Your id number is an int, which is (in this case) formed with 32 bits. This means that the maximum number you can use is 2,147,483,647, and you are using a bigger number.

You should try using long, or a number type that uses more than 32 bits, like long long. This means that the maximum number you can use is 263 - 1 = 9,223,372,036,854,775,807, solving your problem.

So, you should make these changes in your code:

long long id;
printf("Enter the Id: ");
scanf("%lld", &id);

This Page provides a good explanation of the types available in C++.

Check if a number is odd or even in Python

if num % 2 == 0:
pass # Even
else:
pass # Odd

The % sign is like division only it checks for the remainder, so if the number divided by 2 has a remainder of 0 it's even otherwise odd.

Or reverse them for a little speed improvement, since any number above 0 is also considered "True" you can skip needing to do any equality check:

if num % 2:
pass # Odd
else:
pass # Even

How does '&' work in relation to odd and even? In JS

In binary notation the right-most bit is the ones place:

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
...etc

So as you can see every odd number ends in 1 and every even ends in 0.

When you use & you are making a bit-wise and calculation. When you do X & 1, are you comparing each bit of X against 1 or in binary: 00001 (you can keep extending the zeros to the left) and testing wether both bits are 1.

So for example 22 is 10110 in binary 22 & 1 looks each bit and test if both are true:

1 0 1 1 0
0 0 0 0 1 < no bits are 1 in both number
---------
0 0 0 0 0 < all zeros == 0 so 22 is even

23 is 10111:

1 0 1 1 1
0 0 0 0 1 the last bit is one in both numbers
---------
0 0 0 0 1 < 1 so 23 is odd

Since the last bit is always 1 in odd numbers x & 1 will always be one for odd numbers and zero for evens.

How to determines whether two integers are even or odd

Your current code is not checking all four combinations. I would use this logic:

if (num1 + num2) % 2 == 1:
print("One Number is Even and the other is Odd")
elif num1 % 2 == 0 and num2 % 2 == 0:
print("The Numbers are Even")
else:
print("The Numbers are Odd")

Note that the first condition if the above if block rests on that an odd number plus an even number will always result in an odd number, whose mod 2 remainder will be 1.



Related Topics



Leave a reply



Submit