Swift - arithmetic operators example
The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), and modulo(%) operators.
var a = 9.0 var b = 2.0 print("a = \(a), b = \(b) \n") //Add a and b var result_add = a + b print("a + b = \(result_add)") //Subtract b from a var result_sub = a - b print("a - b = \(result_sub)") //Multiply a and b var result_mul = a * b print("a * b = \(result_mul)") //Divide a by b var result_div = a / b print("a / b = \(result_div)") //return division remainder //works only with integral operands var result_modulo = Int(a) % Int(b) print("a % b = \(result_modulo)")
The output of the above code will be:
a = 9.0 , b = 2.0 a + b = 11.0 a - b = 7.0 a * b = 18.0 a / b = 4.5 a % b = 1
❮ Swift - Operators