{}const=>[]async()letfn</>var
Development

🔢 Your first calculator in Lua — learn to program from scratch

Have you just started learning Lua and don't know where to start? Let's write a simple but useful project — an interactive calculator in the terminal.

К

Kodik

Author

2 min read

📘 Level: Beginner

🧠 You will learn:

  • use variables

  • read user input

  • work with numbers

  • apply conditions (if, elseif, else)

🚀 What we will do

In this task, you will create simple calculator, which can add, subtract, multiply and divide two numbers. Everything will happen in the terminal — as in classic console utilities.

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

🔧 Preparation

You can write code directly in the browser, for example here:
👉 https://replit.com/languages/lua

🧱 Step 1: Get input

We will ask the user to enter two numbers and an operation sign:

print("Enter the first number:")
local a = io.read("*n")

print("Enter the second number:")
local b = io.read("*n")

print("Enter operation (+, -, *, /):")
local op = io.read()

🧮 Step 2: Perform the operation

Now we process the input and calculate the result:

if op == "+" then
  print("Result: " .. (a + b))
elseif op == "-" then
  print("Result: " .. (a - b))
elseif op == "*" then
  print("Result: " .. (a * b))
elseif op == "/" then
  if b ~= 0 then
    print("Result: " .. (a / b))
  else
    print("You can't divide by zero!")
  end
else
  print("Unknown operation")
end

✅ Verification

Test the calculator:

  • 10, 5, + → 15

  • 10, 0, / → warning about division by zero

💡 Additional ideas

  • Add a restart of the calculator after completion

  • Make a simple menu before you start

  • Support fractional numbers

🐾 What's next

Now you have your first console utility on Lua! This is a great base for the next step — creating mini-applications or even simple games.

Join our community and learn to program in a cozy atmosphere!

Code 🐾 is our fluffy assistant who helps beginners learn programming easily and with a smile. Together with him, you can take courses in Lua, Python and other languages, creating real projects step by step.

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card