Challenges

# Evens or Odds - you know this one

+6
−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.

Why does this post require moderator attention?
Why should this post be closed?

Clarify input and output methods (2 comments)

+5
−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!

Why does this post require moderator attention?

Link to try regex (1 comment)
+5
−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__.

Why does this post require moderator attention?

Useless comment (1 comment)
+4
−0

# JavaScript (Node.js), 6 bytes

n=>n%2


Try it online!

Basically does what you'd expect.

Why does this post require moderator attention?

You don't need to count the f=. (1 comment)
+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.

Why does this post require moderator attention?

+4
−0

# Japt, 1 byte

u


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

Try it

Why does this post require moderator attention?

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

# Jelly, 1 byte

Ḃ


Try it online!

Builtin. Returns 0 for even and 1 for odd

Why does this post require moderator attention?

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

Why does this post require moderator attention?

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

Why does this post require moderator attention?

+1
−0

# Python 3, 51 bytes

n=int(input())
if n%2==1:print("1")
else:print("0")


Try it online!

Why does this post require moderator attention?

+1
−0

# Ruby, 8 bytes

->n{n%2}


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

->{_1%2}


Try it online!

Why does this post require moderator attention?

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

Why does this post require moderator attention?

+0
−0

# C (clang) + BMPL, 55 bytes

n;main(){scanf("%i",&n);if(n%2==0){say(0)}else{say(1)}}


Try it online!

This is NOT meant to be a golfing language. BMPL can only be used on C, pretty much on clang so I'm not sure if gcc works either.

I'm planning for this language to be completed soon, and you can help too in the linked repository. The compiling is the question though.

I'm figuring out how to make a proper input function at the repository, so help would be appreciated.

How it works:

say() // basic output function that outputs any variable given the source code
main(){} // basic main function syntax of clang golfed
say(1) // outputs 1
if(n%2==0) // checks if input is divisible by 2; if divisible, something specific happens


Originally (ungolfed):

#include <Library/basic.h>

int main(){
scanf("%i",&n); // input for the number
if (n % 2 == 0) {
say(0); // output 0 when it's even
} else {
say(1); // output 1 when it's odd
}
}


# C (clang) + BMPL (including function source), 150 bytes

#define say(X) printf(_Generic((X),\
double:"%f",\
float:"%f",\
char*:"%s",\
int:"%d"\
),(X));
n;main(){scanf("%i",&n);if(n%2==0){say(0)}else{say(1)}}


Try it online!

Why does this post require moderator attention?