Quadratic equation in Lua
Example for versions
Lua 5.0.3,
Lua 5.1.4
*n specifies that a number has to be read. Everything else is quite evident.
local A = io.read('*n')
if A==0 then
io.write('Not a quadratic equation.')
return
end
local B = io.read('*n')
local C = io.read('*n')
D = B*B-4*A*C
if D==0 then
io.write('x = ', -B/2/A)
else if D>0 then
io.write('x1 = ', (-B+math.sqrt(D))/2/A, '\nx2 = ', (-B-math.sqrt(D))/2/A)
else
io.write('x1 = (', -B/2/A, ',', math.sqrt(-D)/2/A, ')\nx2 = (', -B/2/A, ',', -math.sqrt(-D)/2/A, ')\n')
end
end
Comments
]]>blog comments powered by Disqus
]]>