# 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


var QUESTION_ID=113448,OVERRIDE_USER=64499;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){var F=function(a){return a.lang.replace(/<\/?a.*?>/g,"").toLowerCase()},el=F(e),sl=F(s);return el>sl?1:el<sl?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

• 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

# Come Here, 68 bytes

ASKi0CALLi*256iCOME FROM SGN(i//256)1CALL(i//256)iTELL48+(iMOD2)NEXT


Less golfed:

ASK i
0 CALL i*256 i
COME FROM SGN(i//256)
1 CALL (i//256) i
TELL 48 + (i MOD 2) NEXT


COME HERE uses integers internally, but handles input and output as strings; unfortunately, the string encoding it uses really isn't all that helpful for working with input as numbers. In this case, we have to repeatedly divide by 256 until we get a value that is less than 256, in order to get the last character.

If little-endian (rather than big-endian) input is allowed, we can use this shorter version (22 bytes) instead:

ASKiTELL48+(iMOD2)NEXT


Prints 0 for even, and 1 for odd.

# ZX80 (4K ROM) 23 tokenised BASIC bytes

 1 INPUT A
2 PRINT A;":"; NOT A AND 1
3 CONTINUE


This is a very simple solution as in Sinclair ZX80 BASIC, PRINT 111 AND 1 will produce 1 (and therefore any odd number entered will produce 1 because in binary for any odd positive integer the zeroth bit is always set, so PRINT 11 AND 1 will do 00001011 & 00000001 at the processor level more or less) - if we assume even is true then we can use NOT to invert the value, therefor NOT 111 AND 1 will produce 0 or our false value.

Using the CONTINUE command will continue the symbolic listing from line 1. You may also use RUN but both are equal in the basic tokens used.

## ZX80 (4K ROM) 16 tokenised BASIC bytes

If we assume that odd is our true value then we can remove some of the logic as follows (also saving bytes):

 1 INPUT A
2 PRINT A;":";A AND 1
3 RUN


There is one caveat; by using ZX80 BASIC, you may only enter 16-bit wide signed integers, so the range is -32768 to +32767 inclusive.

The same logic will work in Commodore BASIC (C64/VIC-20) so whilst both of the above will work, you may condense it into one line like 0inputa:printa":"aand1:goto.

# MineFriff, 7 bytes

Ii2,%o;


Explanation:

Ii {Change the input mode to (I)nteger and take input}
2, {Push 2 onto the stack}
%o {x % 2 and output}
;   {end prog}

• I love MineFriff from the day I find it by clicking random page (too bad it isn't a 3D language) that is, as long as it does not compete with 1+. – null Aug 18 at 14:00
• @HighlyRadioactive it is 3d if used in free-form mode – Lyxal Aug 18 at 21:45

# Ruby, 6 bytes

n.odd?


Try it online!

I don't know if this is cheating. There's a method definition in the header. Is there a way to skip doing this and pass in a number differently, perhaps?

odd? is a default operation in Ruby.

• Unless there is a way to define anonymous functions in Ruby, I believe this is a snippet, not a complete solution, unfortunately – caird coinheringaahing Oct 5 '19 at 17:49

# 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.

## Kotlin, 15 bytes

{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

## Microsoft Word Math Autocorrect, 40 bytes

0$->$0
1$->$1
2$->$0
3$->$1
4$->$0
5$->$1
6$->$0
7$->$1
8$->$0
9$->$1


Then make a new equation. Separate each digit with a space, or this will not work. When you type your last digit, it will be replaced by either a $1 (if it's odd) or$0 (if it's even).

For example, to input the number 942, type 9 4 2$, which becomes 9 4$1, telling me it's odd. As a bonus, this works for all even bases up to 10.

# MAWP, 36 bytes

%@_1A[1A~25WWM~]%[{1A{1M}<0:.>}2A]1:


## Explanation

It uses the same logic as all the other programs, but MAWP does not have a modulo operator.

The workaround is repeately deducting 2 from the number until we reach 0 or 1.

%@_1A[1A~25WWM~]% snippet used for inputting multi digit numbers
[                 loop if top of stack != 0
{1A{1M}<0:.>}    if number-1=0 print 0 and end program
2A]               subtract 2 from the number
1:                once loop ends, print 1


4 digit and higher numbers take very long to check in the online interpreter.

Try it!

• I've actually made a modulo function that is pretty short in my FizzBuzz answer codegolf.stackexchange.com/a/207427/92080 – Dion Aug 11 at 5:04
• Wow, that's pretty cool. Should put that in the language examples. – Razetime Aug 11 at 5:08

# dotcomma, 74 bytes

[[,.][.][.].,].[[[[,.][[].[],][[].[],].,][[,][[].[],][[].[],].],[],.][.].]


Dotcomma is a language I recently invented, designed to do things in an interesting way with as few instructions as possible. In order to accomplish this, there are two operators (predicatably . and ,) which can do entirely different things depending on context. This answer's explanation will be a somewhat high level explanation of how this program works. To see some simpler examples, check out the examples in the page linked in the title.

The first part of this program, [[,.][.][.].,], adds 2 to the inputted number. The [,.] block will take a number from the queue (which will contain the input), add it to two [.] blocks (equal to 1 each), and put it back in the queue. The . between the two main blocks creates a loop.

To make the loop easier to understand, here's the code broken into more manageable pieces:

[[,.][.][.].,].[
[
[[,.][[].[],][[].[],].,]
[[,][[].[],][[].[],].],[],.
][.].
]


The first part of the loop, on line 3, will take the current number from the queue and subtract 2 from it. It then puts it back in the queue, and the second part tests whether that number minus 2 is negative. If so, the loop ends.

The result is the number in the queue being either 0 if the input was even, or 1 if the input was odd.

# !@#$%^&*()_+, 14 bytes *$$(_^_$_^$)$#


Someone created an Esowiki page for this language and put a badly-golfed parity program there, which motivated me to create this program. It inputs n and inverts 0 n times.

# Aceto, 6 bytes.

%2
rip


ri reads a string and casts it to an integer, 2% does modulo 2, and p prints. 0 stands for even and 1 for odd in the printed result.

• This is very likely the theoretical minimum. – L3viathan May 8 '17 at 8:31
• rip Aceto, postdated the challenge – Matthew Roh May 8 '17 at 8:32
• :) @L3viathan is indeed the theoretical minimum. – Laura Bostan May 8 '17 at 8:33

# PARI/GP, 6 bytes

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.

read()%2


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.

# Applescript function, 26

on f(i)
return i mod 2
end


# Test calls

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.

# Convex, 3 bytes

Ã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.