A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Even Odd in Mathematica page! Here, you'll find the source code for this program as well as a description of how the program works.
(* Code *)
(* The Mathematica built-in EvenQ essentially provides the required behavior;
so the only code needed is the 'user interface' to make it look a little more
like a Unix command-line tool: *)
evenOddMain = Catch[
Module[{fromNegativeDigits},
(* oddly, Mathematica doesn't appear to provide a function which can parse strings representing negative integers *)
fromNegativeDigits = Piecewise[{
{FromDigits[#], StringMatchQ[#, DigitCharacter ..]},
{-FromDigits[StringDrop[#, 1]],
StringMatchQ[#, "-" ~~ DigitCharacter ..]}},
Throw["Usage: please input a number"]
] &;
If[EvenQ[fromNegativeDigits[#]], "Even", "Odd"]]] &;
(* Valid Tests *)
Print /@ evenOddMain /@ {"2", "5", "-14", "-27"};
(* Invalid Tests *)
evenOddMain[""]
evenOddMain["a"]
Even Odd in Mathematica was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.