Evens or Odds - you know this one
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.
Regex, 8 bytes ``` [02468] …
3y ago
[Python 3], 10 bytes …
3y ago
Scala `-language:postfixOps`, …
3y ago
[JavaScript (Node.js)], 6 byte …
3y ago
Japt, 1 byte u Japt, …
3y ago
[Jelly], 1 byte Ḃ Tr …
3y ago
Mathematica, 4 bytes ``` O …
3y ago
Rockstar, 44 bytes Outputs …
3y ago
[MAWP], 8 bytes ``` @!2P2WA: …
3y ago
[Ruby], 8 bytes ```ruby -> …
3y ago
[C (gcc)], 20 bytes …
3y ago
ESCR, 17 bytes show [and …
2y ago
Myby, 2 bytes ``` %2 ``` I …
2y ago
Ruby, 13 bytes According to …
3y ago
[PHP], 36 bytes <?p …
3y ago
[Python 3], 51 25 bytes …
3y ago
16 answers
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.
0 comment threads
Python 3, 10 bytes
1 .__and__
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__
.
Regex, 8 bytes
[02468]$
Matches strings ending in one of 0, 2, 4, 6, 8. [13579]$
is equivalent but with opposite outputs.
Scala -language:postfixOps
, 2 bytes
1&
1 for odd, 0 for even.
Scala, 3 bytes
_%2
1 for odd, 0 for even.
0 comment threads
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.
0 comment threads
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)
0 comment threads
Ruby, 8 bytes
->n{n%2}
Generic mod-in-a-lambda solution. Alternately, for 2.7+:
->{_1%2}
0 comment threads
C (gcc), 20 bytes
f(a){puts("!"+a%2);}
Function solution. Prints !
in case of even numbers, otherwise just new line.
0 comment threads
Python 3, 51 25 bytes
n=int(input());print(n%2)
Golfed 26 bytes using the technique from my PHP answer.
0 comment threads
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
0 comment threads
PHP, 36 bytes
<?php $x=(int)fgets(STDIN);echo$x%2;
Outputs 1
if odd and 0
if even.
1 comment thread