# Is this even or odd?

Note: There is not been a vanilla parity test challenge yet (There is a C/C++ one but that disallows the ability to use languages other than C/C++, and other non-vanilla ones are mostly closed too), So I am posting one.

Given a positive integer, output its parity (i.e. if the number is odd or even) in truthy/falsy values. You may choose whether truthy results correspond to odd or even inputs.

# Examples

Assuming True/False as even and odd (This is not required, You may use other Truthy/Falsy values for each), responsively:

(Input):(Output)
1:False
2:True
16384:True
99999999:False

• This isn't the first time I've confused mathematical with computational parity... this is a code site after all! – Neil Mar 21 '17 at 10:38
• Since this is pretty much one of these(1,2,3) questions, it should probably have a snippet to see all the answers. – fəˈnɛtɪk Mar 21 '17 at 13:57
• @MikeBufardeci Because "catalogue" is spelled differently based on which country you're from. For those of us in the U.S., it's "catalog". "Leaderboard" is culture-invariant. – mbomb007 Mar 21 '17 at 16:37
• @tuskiomi The challenge only asks about positive integers. (0 is considered even but not positive) – Calvin's Hobbies Mar 22 '17 at 2:46
• @LucioCrusca Welcome to PPCG! The basic idea of Code Golf is to make a program in the shortest form you can. This challenge is to read an integer (positive,non-zero), and output if it is even or odd. If you are confused with something, please visit The Nineteenth Byte and ask freely. Or if you are confused with the site's policy or rules, go to the Meta. Finally, Thanks for subscribing to our community! – Matthew Roh Mar 23 '17 at 15:09

# Commodore BASIC (C64/128, VIC-20, TheC64/Mini, C16/+4) - 21 tokenized bytes

In this one, odd numbers produce a TRUE value and even numbers produce a FALSE (though on Commodore BASIC, FALSE is zero and TRUE is negative one; it does not do this, more conforms to modern-days TRUE/FALSE integer equivalents)

0 inputa:ifatH?a":"aaN1

Expanded and non-minimised as:

0 input a
1 if a then print a; ":"; a and 1

# MarioLANG, 37 bytes

;>-)+([!)
="=====#:
)![(-)-<
:#====="

Try it online!

Outputs 1 for odd and 0 for even.

{n:Int->n%2<1}

## W, 2 bytes

2m

Basically just modulo by 2...

# Python 3, 12 bytes

lambda n:n&1

Outputs 1 for odd, 0 for even.

# 05AB1E, 1 byte

É

Try it online!

## Explanation

# implicit input
É  # push 1 if odd, otherwise push 0

# Brainetry-w 2, 25 bytes

Golfed version:

a b c d e f
a b c d e f g

How it works: we set the cell size to 2 with -w 2 which pretty much does all the """heavy""" lifting for us. After that we just input one number and output it. If you want to input an actual number instead of an ASCII code point you can also set the --numeric-io flag.

The golfed version was adapted from the program at the end of this answer. To try this online you can

• head over to this replit link, copy&paste the code into the btry/replit.btry file and hit the green "Run" button;
• clone the github repo and from your terminal run ./brainetry btry/ppcg/even_or_odd.btry --numeric-io -w 2.
Let me read one input number
and now I will ouput its parity.

# Pyramid Scheme, 108 92 bytes

^
/-\
^---^
-^ ^-
-^-
/^\
^---^
-^ /#\
/-\---^
/ 1 \ /l\
-----/ine\
-----

Try it online!

Outputs -2 for odd numbers and 0 for even ones. This can be swapped by switching the top pyramid for + instead of -. This is basically equivalent to (-1)**int(input) - (-1)**0

n->n%2

Mod 2.

# CJam, 4 bytes

q~2%

Try it online!

Simple mod by 2. Outputs 1 for odd, 0 for even.

# Java, 91 bytes

enum d{;public static void main(String[]a){System.out.println(Long.parseLong(a[0])%2==0);}}

Takes number as first command line argument and outputs to STDOUT.

## bc, 9 bytes

Output is 1 for odd, and 0 for even.

Try it online!

A trailing newline is necessary to run successful, and it is included in the bytes count. The read() function works only for numerical input.

on f(i)
return i mod 2
end

log f(1)
log f(2)
log f(3)

# Go, 26 bytes

func(i int)int{return^i&1}

Returns 1 for even and 0 for odd.

Try it online!

# Scala, 15145 3 bytes

_%2

Returns 0 if it's an even number and 1 if it's odd

This is equivalent to (i:Int)=>i%2.
However, this needs to be assigned to a variable of type Int=>Int, otherwise the compiler will complain.
The whole code would look like this:

val x: Int => Int = _%2
• Just a tip but you can reduce this by one byte by doing <1 instead of ==0. – Stefan Aleksić Mar 25 '17 at 14:46
• I don't think that's cheating. See here. Notice that Java 8 lambda solutions often leave off the type. – Brian McCutchon Mar 26 '17 at 23:44
• Is the <1 even needed? would it not return 0 if even and 1 if odd? – fəˈnɛtɪk Mar 27 '17 at 14:49
• Depends if we need a boolean, but I guess returning 0 and 1 works too. Thanks! – 2xsaiko Mar 27 '17 at 19:37

# Swift, 18 bytes

let f={n in n%2<1}
• Can be shortened to let f={n in n%2} which would return 0 for even and 1 for odd, saving 2 bytes – Mr. Xcoder Apr 11 '17 at 14:09

# Chip, 2 bytes

For the simple case:

Aa

This only handles one-byte numbers, either a single ASCII digit or a 1 byte unsigned integer (because ASCII code points for odd digits are themselves odd). This works like the &1 bitmask used in many other answers. If multiple bytes are provided, this will treat each new byte as a separate input. Outputs 0x0 for even and 0x1 for odd.

By adding e*f, we can get output in ASCII (5 bytes):

Aae*f

Try it online!

# 23 + 3 = 26 bytes

For the complex case:

Svvvvvvvv~t
azABCDEFGH

This handles numbers of any length, ASCII or unsigned integer as before (big endian so that least significant bit is still last). Requires either a null terminator, or the -z flag (as shown in the byte count) to detect the end on the input. Like above, outputs 0x0 for even and 0x1 for odd.

Again, we can add e*f to get ASCII output (25 + 3 = 28 bytes):

*fSvvvvvvvv~t
eazABCDEFGH

Try it online!

# x86 machine code, 2 bytes

00000000  a8 01                                             |..|
00000002

Sets the zero flag if the value in register A is even.

# R, 10 bytes

!scan()%%2

If you can get away with assuming n is the value, !n%%2 works.

Ã2%

Try it online!

# ///, 5 bytes

/00//

Try it online!

Takes input in unary. Input goes after the last /. Prints a 0 of it's even, otherwise prints nothing. Incredibly short for a /// program that actually does soething.

# x86 machine code, 3 bytes

83 E0 01 (and ax, 1 in nasm)

Explanation:

Anding any number with 1 will return 1 if the number is odd, or 0 if it is even.

1111 (15)
&0001
-----
0001

1110 (14)
&0001
-----
0000

0001 (1)
&0001
-----
0001

0010 (2)
&0001
-----
0000

So move a number into AX, use the three bytes, and AX will hold 0 or 1, if the number is even or odd.

# Python 2, 16 bytes

print(input()%2)

Try it online!

• Oh I thought you were using Python Two-Thirds – Matthew Roh Apr 1 '17 at 0:15
• This code will spit me an error message. – Dat Apr 16 '17 at 23:46
• this code is python 2 only, in python 3 it would be: print(int(input())%2) – Felipe Nardi Batista May 4 '17 at 11:09
• Save a byte with print input()%2 – Josh May 19 '17 at 21:37

# Python 3, 21 bytes

print(int(input())%2)

Try it online!

• Hello, and welcome to PPCG! This is 17 bytes. However, you need some way of outputting it (i.e. print). – NoOneIsHere Apr 16 '17 at 23:21
• @NoOneIsHere, may I know how did you calculate the size? – Dat Apr 16 '17 at 23:22
• You can calculate the size for example with this online tool. In addition, if there aren't any special Unicode characters in the code, the size in bytes is usually the number of characters. – Steadybox Apr 16 '17 at 23:41
• You can use Python 2 for a few less bytes: print input()%2 is 15 bytes. – NoOneIsHere Apr 17 '17 at 0:10

# J-uby, 5 Bytes

~:%&2

Takes the reversed-arguments version of % and partially applies 2 to it, creating a function which takes x and returns x%2. 0 for even, 1 for odd.

# Casio-Basic, 9 bytes

mod(n,2)

8 bytes for the function, 1 to add n as an argument.

# JavaScript, 6 bytes

A function of input n that returns 0 if even and 1 if odd.

n=>n%2

# Triangular, 6 bytes

$.2%m< Prints 0 for even, 1 for odd. Try it online!$
. 2
% m <

\$ reads an integer to the stack. 2 pushes 2 to the stack. m divides the top two stack values and pushes the remainder. % prints as an integer.

oddp