Your task is to create an interpreter for a calculator. It has to parse commands like 5 + 5 and return the answer.

It has to be able to add, subtract, multiply, and divide. The interpreter only needs to accept two numbers (ex. 9 * 5 and 3 + 3)

The interpreter will take input from a console/whatever, not a file.

Eval isn't allowed


Input: 5 * 5

Output: 25

Input: 8 / 2

Output: 4


If you're really bored you can add these to the interpreter, if you'd like:

Simple mathematical functions like sqrt.

Make the interpreter be able to accept more than two numbers (ex. 55 + 55)

  • 1
    \$\begingroup\$ Only cursory check, but duplicate? codegolf.stackexchange.com/questions/568/basic-calculator If not, I think you should quantify the bonus. \$\endgroup\$
    – cole
    Oct 6, 2015 at 4:47
  • \$\begingroup\$ I'd almost say this is a dupe of this challenge, but at the same time I feel that accepting only two numbers might make a difference. Either way, even if this isn't a dupe, the spec needs a bit more fleshing out: 1) Is eval allowed? 2) Can numbers be more than one digit? \$\endgroup\$
    – Sp3000
    Oct 6, 2015 at 4:48
  • 1
    \$\begingroup\$ What should 8 / 3 print? \$\endgroup\$
    – Dennis
    Oct 6, 2015 at 4:54
  • \$\begingroup\$ Does the bonus have a byte value/% taken off the byte count? \$\endgroup\$
    – lirtosiast
    Oct 6, 2015 at 5:02
  • \$\begingroup\$ By the way, we have a Sandbox where you can post challenge ideas to get feedback from communinity before "going live". \$\endgroup\$
    – Dennis
    Oct 6, 2015 at 5:20

5 Answers 5


Pyth, 1 byte


For some reason, multiplication and division don't work in the online interpreter. The Python interpreter behaves as expected.


Perl, 10 8 chars

I hope this is not cheating ;)

8 chars (7 chars code + 1 for -p): Thanks to Dennis

perl -pe '$_=eval'

10 chars:

say eval<>

Takes input from STDIN and needs perl v5.10 and above.


><>, 14 Bytes

Thanks @Sp3000 for saving 2 bytes

Quick, but kinda cool solution. Probably not an ideal golf, but utilizes an interesting feature of ><>.

<v ip02ii

Receives input through the numerical interpretation of ascii characters (i.e. a is 97). It places the second character read (the operator) in the space after <v. Because dividing in ><> uses ,, not /, I make use of the fact that / reflects the pointer to accommodate for that case.

Alternate solution if that's invalid (+4 Bytes):

<v %cip02i%ci

For this we just take mod 12 (c pushes 12) of the input since 0 is 48 and there are only 10 characters.

If , could be used in place of /, this would be a lot shorter (something like ii60pi n; or ic%ia0pic% n;)

Golf suggestions gladly welcome -- I'm rather tired right now.

  • \$\begingroup\$ I'm still not sure whether the OP intended to allow eval-like submissions, but in any case the last line can just be n<,; \$\endgroup\$
    – Sp3000
    Oct 6, 2015 at 5:24

JavaScript ES6, 10 4 bytes

Anonymous function.

  • 2
    \$\begingroup\$ eval is already assignable (e.g., f=eval), so I think you can claim a 4 byte solution. \$\endgroup\$
    – Dennis
    Oct 6, 2015 at 5:05
  • \$\begingroup\$ Couldn't this technically be zero bytes? Just say 'call the function with the name eval to get the answer', the same way you could normally say 'call the function f'. \$\endgroup\$ Aug 2, 2019 at 12:27

MATLAB, 4 bytes (or 0 bytes)

eval s

Pretty simple really. MATLAB will evaluate the string s you pass it, and voila. For example the following operators work: * / + - ^ sqrt() min() max(). Well pretty much anything that you can think of.

Heck you can even print Hello World! if you want - eval 'disp(''Hello World!'')'.

To be honest given that MATLAB is already a calculator, you can call it 0 if you omit eval and simply type in the string:

>> 8/2
ans =

Not the answer you're looking for? Browse other questions tagged or ask your own question.