Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Evens or Odds - you know this one

+7
−0

Get ready for a comparatively dry question - this is intended to be one of the "the"s of the code-golfing dictionary.

Create a program which inputs a base 10 non-negative whole number (without leading 0s) and outputs something if the number is odd or something else if the number is even.

You MUST input through STDIN unless your language doesn't have input facilities, in which case you may make a function for the purpose.

The odd and even outputs can be whatever you want, so long as the output for a specific parity will stay the same no matter what number of that parity is inputted.

One of the outputs can be absolutely nothing (0 characters) so long as the other output isn't nothing - one or both of the outputs can be printed through STDERR as long as the erroring output is constant for its parity (doesn't change depending on what numbers of a parity you feed it).

Shortest code following the rules wins.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Clarify input and output methods (2 comments)

16 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

Ruby, 8 bytes

->n{n%2}

Generic mod-in-a-lambda solution. Alternately, for 2.7+:

->{_1%2}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+6
−0

Python 3, 10 bytes

1 .__and__

Try it online!

Returns 1 for odd numbers and 0 for even numbers.

Two bytes shorter than the trivial lambda x:x%2 and one byte shorter than the equivalent magic function 2 .__rmod__.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Useless comment (1 comment)
+6
−0

Regex, 8 bytes

[02468]$

Matches strings ending in one of 0, 2, 4, 6, 8. [13579]$ is equivalent but with opposite outputs.

Try it on Regex101!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Link to try regex (1 comment)
+4
−0

JavaScript (Node.js), 6 bytes

n=>n%2

Try it online!

Basically does what you'd expect.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

You don't need to count the `f=`. (1 comment)
+4
−0

Jelly, 1 byte

Try it online!

Builtin. Returns 0 for even and 1 for odd

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Scala -language:postfixOps, 2 bytes

1&

Try it online!

1 for odd, 0 for even.

Scala, 3 bytes

_%2

Try it online!

1 for odd, 0 for even.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Japt, 1 byte

u

Japt, more like JABT (just another builtin) :P

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Or, alternatively, `v`. (1 comment)
+2
−0

Mathematica, 4 bytes

OddQ

Not at all my code - this is the original post by Martin Ender. Prints True for odd inputs and False for even inputs.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

C (gcc), 20 bytes

f(a){puts("!"+a%2);}

Try it online!

Function solution. Prints ! in case of even numbers, otherwise just new line.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Rockstar, 44 bytes

Outputs 1 for odd or 0 for even.

listen to N
let M be N/2
turn up M
say M*2-N

Try it here (Code will need to be pasted in)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

MAWP, 8 bytes

@!2P2WA:

Try it!

1 for odd and zero for even.

mawp doesn't have modulus, so it floor divides by 2, multiplies by 2 and subtracts from the input.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

PHP, 36 bytes

<?php $x=(int)fgets(STDIN);echo$x%2;

Try it online!

Outputs 1 if odd and 0 if even.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Myby, 2 bytes

%2

Input is taken from the command line implicitly. 0 for even, 1 for odd. Since Myby is still in its early stages, there is no online sandbox. Some test cases can be seen here.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Python 3, 51 25 bytes

n=int(input());print(n%2)

Try it online!

Golfed 26 bytes using the technique from my PHP answer.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

ESCR, 17 bytes

show [and [in] 1]

ESCR has no way to read from standard input (currently), so I assumed the existence of the function IN which magically returns the user input. Since the user input was specified to be an integer, the built-in AND function will do a bit-wise logical AND with the input number and 1. The result is 0 for even numbers and 1 for odd. The SHOW command writes the result of the AND function to standard output.

The AND function will bomb the program with an error if the user does not enter an integer.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 13 bytes

According to the rules, programs should read from STDIN and output to the STDOUT. This is my solution:

p gets.to_i&1

Try this online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »