-5

Not a request for code of any type.

Let's say you are randomly generating a number between zero and ten and are doing so until you receive just an even number. Is zero an available result?

Are the evens 0-10:

2, 4, 6, 8, 10

-OR-

0, 2, 4, 6, 8, 10
elixenide
  • 44,308
  • 16
  • 74
  • 100
user3223880
  • 65
  • 1
  • 10
  • This may be worth a read http://en.wikipedia.org/wiki/Parity_of_zero – Scuzzy May 12 '15 at 01:40
  • 2
    I'm voting to close this question as off-topic because it's about math, not programming. See [this question on Math Stack Exchange](https://math.stackexchange.com/questions/15556/is-zero-odd-or-even). – Jeffrey Bosboom Jun 23 '15 at 19:21

3 Answers3

7

Mathematically, 0 is even. PHP has no built-in notion of even.

A (mathematically correct) test for even parity is checking if the number vanishes mod 2. This test is implemented as $number % 2 == 0. For zero you can quickly check that 0 mod 2 is 0, and would be considered even.

Alfred Rossi
  • 1,942
  • 15
  • 19
3

Zero is an even number. So, yes, it's even "in PHP," too. As a test, 0 % 2 === 0 => 0 is even.

elixenide
  • 44,308
  • 16
  • 74
  • 100
1

This might be a month late but the best way to determine if a number is even or odd is by using a simple mathematical formula.

Even: 2n or 2 x n;
Odd: 2n+1 or (2 x n)+1;

Using the two equations above (where n>=0), you can determine that 0 is even.

Even: 2(0) = 0;
Odd: 2(0)+1 = 1;

So using the lowest possible number where n is greater than or equal to zero, it is proven that 0 is even.