# Simple four-operation calculator in Lua

I'm a beginner at Lua and programming in general and I just made a simple calculator and I was wondering where I could make improvements.

print("Choose numbers:")
print("Num1:")
print("num2:")
print("Choose operator: + / - / * / /")
if operator == "+" then
local add = num1 + num2
end
if operator == "-" then
local subtract = num1 - num2
print(subtract)
end
if operator == "*" then
local multiply = num1 * num2
print(multiply)
end
if operator == "/" then
local devide = num1 / num2
print(devide)
end

• What exactly do you want to improve? – user6245072 May 26 '16 at 20:16

Try some features, typically available from functional languages. For example, functions that is first class values.

local operators = {
["+"] = function(x,y) return x+y end,
["-"] = function(x,y) return x-y end,
["*"] = function(x,y) return x*y end,
["/"] = function(x,y) return x/y end
}

local function default()
print "No such operator"
end

print("Choose numbers:")
print("Num1:")