← Back to Scala Course | Chapter 3: Operators & Expressions | Lesson 1 of 7

Arithmetic Operators

In this page:

  1. Arithmetic Operators

Arithmetic Operators

Scala provides +, -, *, /, and % for arithmetic, all implemented as methods on numeric types (Scala has no truly special-cased operators). Integer division truncates any fractional part, while dividing two Doubles keeps precision. % returns the remainder of a division.

Warning: Dividing two Ints, like 7 / 2, gives 3, not 3.5 -- make at least one operand a Double to get a fractional result.

Example: Arithmetic Operators

markup
object Main extends App {
  val a = 7
  val b = 2

  println(a + b)
  println(a - b)
  println(a * b)
  println(a / b)          // integer division
  println(a % b)          // remainder
  println(a.toDouble / b) // fractional division
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.