Menu
Coddy logo textTech

Recap - Sum of Even Numbers

Part of the Fundamentals section of Coddy's Lua journey — lesson 90 of 90.

challenge icon

Challenge

Easy

Write a script that calculates the sum of all even numbers from 1 to 20 using a for loop, conditional logic, and variable accumulation.

Create a local variable named total and initialize it to 0. Then use a numeric for loop to iterate through all numbers from 1 to 20.

Inside the loop:

  • Use the modulo operator (%) to check if the current number is even
  • If the number is even, add it to your running total

After the loop completes, print the final sum of all even numbers.

Try it yourself

-- Initialize the total variable
local total = 0

-- TODO: Write your code here
-- Use a for loop to iterate from 1 to 20
-- Check if each number is even using the modulo operator (%)
-- Add even numbers to the total

-- Output the result
print(total)

All lessons in Fundamentals