Programming Fundamentals/Conditions/Lua: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
Creating |
(No difference)
|
Revision as of 04:23, 3 February 2017
conditions.lua
-- This program asks the user to select Fahrenheit or Celsius conversion
-- and input a given temperature. Then the program converts the given
-- temperature and displays the result.
function to_celsius()
local f
local c
io.write("Enter Fahrenheit temperature:", "\n")
f = tonumber(io.read())
c = (f - 32) * 5 / 9
io.write(f, "° Fahrenheit is ", c, "° Celsius", "\n")
end
function to_fahrenheit()
local c
local f
io.write("Enter Celsius temperature:", "\n")
c = tonumber(io.read())
f = c * 9 / 5 + 32
io.write(c, "° Celsius is ", f, "° Fahrenheit", "\n")
end
function main()
local choice
io.write("Enter F to convert to Fahrenheit or C to convert to Celsius:", "\n")
choice = io.read()
if choice == "C" or choice == "c" then
to_celsius()
elseif choice == "F" or choice == "f" then
to_fahrenheit()
else
io.write("You must enter C to convert to Celsius or F to convert to Fahrenheit!", "\n")
end
end
main()
Try It
Copy and paste the code above into one of the following free online development environments or use your own Lua compiler / interpreter / IDE.