Scala Tutorial Scala References

Scala - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), and modulo(%) operators.

object MainObject {
  def main(args: Array[String]) {
    var a = 9.0
    var b = 2.0

    println(s"a = ${a}, b = ${b}\n")

    //Add a and b
    var result_add = a + b
    println(s"a + b = ${result_add}")

    //Subtract b from a
    var result_sub = a - b
    println(s"a - b = ${result_sub}")

    //Multiply a and b
    var result_mul = a * b
    println(s"a * b = ${result_mul}")

    //Divide a by b
    var result_div = a / b
    println(s"a / b = ${result_div}")

    //return division remainder
    var result_modulo = a % b
    println(s"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.0

❮ Scala - Operators

5