Middle three digits: Difference between revisions

Content deleted Content added
Line 468:
-2002: Invalid length 4
0: Invalid length 1</pre>
 
 
=={{header|Lua}}==
<lang lua>function middle_three(n)
if n < 0 then
n = -n
end
n = tostring(n)
if #n % 2 == 0 then
return "Error: the number is even."
elseif #n < 3 then
return "Error: the number has less than 3 digits."
end
 
local l = math.floor(#n/2)
return n:sub(l, l+2)
end
 
-- test
do
local t = {123, 12345, 1234567, 987654321,
10001, -10001, -123, -100, 100, -12345, 1,
2, -1, -10, 2002, -2002, 0}
 
for _,n in pairs(t) do
print(n, middle_three(n))
end
end</lang>
 
{{out}}
<pre>123 123
12345 234
1234567 345
987654321 654
10001 000
-10001 000
-123 123
-100 100
100 100
-12345 234
1 Error: the number has less than 3 digits.
2 Error: the number has less than 3 digits.
-1 Error: the number has less than 3 digits.
-10 Error: the number is even.
2002 Error: the number is even.
-2002 Error: the number is even.
0 Error: the number has less than 3 digits.</pre>
 
=={{header|Mathematica}}==