Pages

Tuesday, June 07, 2016

Lua basics 01

Calculate factorial of a number.
-- Calculate factorial function factorial(number) if number == 0 then return 1 else return number * factorial(number - 1) end end -- Main function function main() io.write("Enter the number to get the its factorial value: ") userInput = io.read("*number") if userInput == nil then print("User input is invalid.") os.exit() end number = factorial(userInput) io.write("factorial of ", userInput, ": ", number, "\n") end main()

No comments: