Elixir - Arithematic Operators



The following table shows all the arithmetic operators supported by Elixir language. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example
+ Adds 2 numbers. A + B will give 30
- Subtracts second number from first. A-B will give -10
* Multiplies two numbers. A*B will give 200
/ Divides first number from second. This casts the numbers in floats and gives a float result A/B will give 0.5.
div This function is used to get the quotient on division. div(10,20) will give 0
rem This function is used to get the remainder on division. rem(A, B) will give 10

Example

Try the following code to understand all arithmetic operators in Elixir.

a = 10
b = 20

#Addition
IO.puts("Addition " <> to_string(a+b))

#Subtraction
IO.puts("Subtraction " <> to_string(a-b))

#Multiplication
IO.puts("Multiplication " <> to_string(a*b))

#Division
IO.puts("Division " <> to_string(a/b))

#Integer division
IO.puts("Integer division " <> to_string(div(a,b)))

#Modulo
IO.puts("Modulo " <> to_string(rem(a,b)))

The above program generates the following result −

Addition 30
Subtraction -10
Multiplication 200
Division 0.5
Integer division 0
Modulo 10
elixir_operators.htm
Advertisements