Recap - Sum of Even Numbers
Part of the Fundamentals section of Coddy's Lua journey — lesson 90 of 90.
Challenge
EasyWrite 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
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator