The shortest code to tell if a number is even or odd

4

1

One of my colleagues proposed us a challenge: to write the shortest C/C++ program to determine if a number is even or odd.

These are the requirements:

  • Get a number from keyboard, at run-time (not from command line)
  • Output the string pari if the number is even
  • Output the string dispari if the number is odd
  • We can assume that the input is legal
  • We can assume that the machine has a i386 architecture
  • Code preprocessing is not allowed (e.g.: use #defines)
  • [edit after comments] The code can be considered valid if it compiles with gcc 4.x

This is the best attempt we had so far (file test.c, 50B):

main(i){scanf("%d",&i);puts("dispari"+3*(~i&1));}

Do you think we can go further?

(*): "dispari" is the italian for odd and "pari" is the italian for even

Vincenzo Pii

Posted 2012-10-09T19:55:04.137

Reputation: 167

2Is this for C or C++? In C++, that code is invalid. I believe in C as well.\ – Luchian Grigore – 2012-10-09T19:56:03.823

@LuchianGrigore, you can use both C and C++. The choice depends on the language that lets you write the shortest code. The code above compiles correctly with gcc, maybe I should add that as a requirement. – Vincenzo Pii – 2012-10-09T20:01:42.300

1Sorry, but your best example doesn't compile with gcc 4.6.3 (Edit: it does) – Pieter Bos – 2012-10-09T20:06:04.260

@niomaster, I have the same version and it works, but not if the file extension is different than .c. Anyway, if you think that doesn't work, you could try providing your shortest working solution :). – Vincenzo Pii – 2012-10-09T20:07:04.413

3Slight variation (untested): use return value of scanf to shorten into main(i){puts("dispari"+3*(scanf("%d",&i)&~i));}. – Howard – 2012-10-09T20:12:55.970

@Howard that works with gcc 4.6.3! – Vincenzo Pii – 2012-10-09T20:17:56.117

@Jack, my solution is C, yours could be C++, hence the tag. We are not interested in warnings here, this is just a stupid competition between too nerdish colleagues! – Vincenzo Pii – 2012-10-09T20:52:51.427

@VincenzoPii: Ok. Removed comment. I'm sorry, I'm in the sense of stackoverflow.com. – Jack – 2012-10-09T21:51:44.753

1You can't legally and portably perform output without either #include <stdio.h> (which is a preprocessor directive) or your own declaration of the routines you're using. As of C90, calling an undeclared function has defined behavior only in narrow circumstances; as of C99, it's a constraint violation (C's version of "illegal"). – Keith Thompson – 2012-10-10T00:40:31.503

Why no other languages allowed?! I like Perl so much ._. print<><0?dis:$,,pari – Leo Pflug – 2014-02-13T10:07:24.150

Answers

4

39 bytes

Given you didn't specified the range of the number input, then here's a solution that handles 0..9 only:

main(){puts("dispari"-~getchar()%2*3);}

baby-rabbit

Posted 2012-10-09T19:55:04.137

Reputation: 1 623

8

47 bytes (2 less)

main(i){scanf("%d",&i);puts("dispari"-~i%2*3);}

For a positive i, ~i is negative, so ~i%2 is 0 or -1.

ugoren

Posted 2012-10-09T19:55:04.137

Reputation: 16 527

3

48 bytes

main(i){scanf("%d",&i);puts("dispari"+3-i%2*3);}

Benoit Thiery

Posted 2012-10-09T19:55:04.137

Reputation: 131

1

34 bytes

Build with

gcc -nostartfiles -Wl,-ea odd.c

Works in GCC 6.3.0.

a(){puts("dispari"-~getch()%2*3);}

peter ferrie

Posted 2012-10-09T19:55:04.137

Reputation: 804