Skip to main content
edited tags
Link
Doorknob
  • 72.1k
  • 20
  • 146
  • 393
Tweeted twitter.com/#!/StackCodeGolf/status/256092203447881728
added 89 characters in body
Source Link

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

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)

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

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

Post Migrated Here from stackoverflow.com (revisions)
Source Link

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

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)

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