Commodore C64 BASIC,
86 83 BASIC Bytes used
This method in Commodore BASIC uses the
def fn function, which is somewhat limited but can be useful. My favourite YouTube Channel 8-BIT Show and Tell talks about this and other rarely used commands in some depth here. If you are interested, it's worth mulling over.
How it works
The function that I've defined (referenced as
fnm(x)) simply acts like a modulus operator in modern days computer programming languages like C and C++. In this instance, we are returning the remainder of any division by 2.
inputx waits for a numeric input, which can be an integer or a floating point number (in fact, Commodore BASIC only really deals with floating point numbers in the ROM, but that's for another time). So note that I've not included a sanity check for floats, nor for
0, which will be an endless loop.
Once you enter a value,
y=-(fnm(x)=0) is a short-hand any byte-saving way of
if fn m(x) = 0 then let y = 0: end: else let y = 1. Note that like many BASIC dialects, Commodore BASIC uses a single equals for assignment and comparison. Commodore BASIC also returns
false, as we want to initialise
y with the value of
1 if the number entered is an even number, so it will divide equally by two at least once. So, now if
0 and the number entered is odd, then we print it and end.
In line 1, we have a loop which divides the number entered into
x by 2; if that is even, then we increase the
y counter by one and go back to the start of line 1 to divide
x by 2 again, and so on until
x is odd. On this note the loop ends and
y is printed.