Arithmetic/Integer: Difference between revisions
Content added Content deleted
(added ol) |
(→{{header|Elixir}}: add Integer.floor_div, Integer.mod and output sample) |
||
Line 981: | Line 981: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{works with|Elixir|1.4}} |
|||
<lang Elixir># Function to remove line breaks and convert string to int |
|||
<lang Elixir>defmodule Arithmetic_Integer do |
|||
get_int = fn msg -> IO.gets(msg) |> String.strip |> String.to_integer end |
|||
# Function to remove line breaks and convert string to int |
|||
defp get_int(msg) do |
|||
# Get user input |
|||
IO.gets(msg) |> String.strip |> String.to_integer |
|||
a = get_int.("Enter your first integer: ") |
|||
end |
|||
b = get_int.("Enter your second integer: ") |
|||
def task do |
|||
# Get user input |
|||
a = get_int("Enter your first integer: ") |
|||
b = get_int("Enter your second integer: ") |
|||
IO.puts "Elixir Integer Arithmetic:\n" |
|||
IO.puts "Sum: #{a + b}" |
|||
IO.puts "Difference: #{a - b}" |
|||
IO.puts "Product: #{a * b}" |
|||
IO.puts "True Division: #{a / b}" # Float |
|||
IO.puts "Division: #{div(a,b)}" # Truncated Towards 0 |
|||
IO.puts "Floor Division: #{Integer.floor_div(a,b)}" # floored integer division |
|||
IO.puts "Remainder: #{rem(a,b)}" # Sign from first digit |
|||
IO.puts "Modulo: #{Integer.mod(a,b)}" # modulo remainder (uses floored division) |
|||
IO.puts "Exponent: #{:math.pow(a,b)}" # Float, using Erlang's :math |
|||
end |
|||
end |
|||
Arithmetic_Integer.task</lang> |
|||
IO.puts "Elixir Integer Arithmetic:\n" |
|||
IO.puts "Sum: #{a + b}" |
|||
{{out}} |
|||
IO.puts "Difference: #{a - b}" |
|||
<pre style="height: 80ex; overflow: scroll"> |
|||
IO.puts "Product: #{a * b}" |
|||
C:\Elixir>elixir Arithmetic_Integer.exs |
|||
IO.puts "True Division: #{a / b}" # Float |
|||
Enter your first integer: 7 |
|||
IO.puts "Division: #{div(a,b)}" # Truncated Towards 0 |
|||
Enter your second integer: 3 |
|||
IO.puts "Remainder: #{rem(a,b)}" # Sign from first digit |
|||
Elixir Integer Arithmetic: |
|||
IO.puts "Exponent: #{:math.pow(a,b)}" # Float, using Erlang's :math</lang> |
|||
Sum: 10 |
|||
Difference: 4 |
|||
Product: 21 |
|||
True Division: 2.3333333333333335 |
|||
Division: 2 |
|||
Floor Division: 2 |
|||
Remainder: 1 |
|||
Modulo: 1 |
|||
Exponent: 343.0 |
|||
C:\Elixir>elixir Arithmetic_Integer.exs |
|||
Enter your first integer: -7 |
|||
Enter your second integer: 3 |
|||
Elixir Integer Arithmetic: |
|||
Sum: -4 |
|||
Difference: -10 |
|||
Product: -21 |
|||
True Division: -2.3333333333333335 |
|||
Division: -2 |
|||
Floor Division: -3 |
|||
Remainder: -1 |
|||
Modulo: 2 |
|||
Exponent: -343.0 |
|||
C:\Elixir>elixir Arithmetic_Integer.exs |
|||
Enter your first integer: 7 |
|||
Enter your second integer: -3 |
|||
Elixir Integer Arithmetic: |
|||
Sum: 4 |
|||
Difference: 10 |
|||
Product: -21 |
|||
True Division: -2.3333333333333335 |
|||
Division: -2 |
|||
Floor Division: -3 |
|||
Remainder: 1 |
|||
Modulo: -2 |
|||
Exponent: 0.0029154518950437317 |
|||
C:\Elixir>elixir Arithmetic_Integer.exs |
|||
Enter your first integer: -7 |
|||
Enter your second integer: -3 |
|||
Elixir Integer Arithmetic: |
|||
Sum: -10 |
|||
Difference: -4 |
|||
Product: 21 |
|||
True Division: 2.3333333333333335 |
|||
Division: 2 |
|||
Floor Division: 2 |
|||
Remainder: -1 |
|||
Modulo: -1 |
|||
Exponent: -0.0029154518950437317 |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |