Menu
Coddy logo textTech

Arithmetic Operators

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 19 of 93.

Kotlin provides arithmetic operators to perform mathematical calculations on numbers. These are the same operators you'd use in basic math.

Here are the four fundamental arithmetic operators:

OperatorOperationExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33

You can use these operators with variables and store results:

fun main() {
    val a = 15
    val b = 4
    
    val sum = a + b        // 19
    val difference = a - b // 11
    val product = a * b    // 60
    val quotient = a / b   // 3
    
    println(quotient)
}

Important: When dividing two integers, Kotlin performs integer division: the result is truncated, not rounded. So 15 / 4 gives 3, not 3.75.

If you need decimal results, at least one number must be a Double:

fun main() {
    println(15.0 / 4)  // Prints: 3.75
}
challenge icon

Challenge

Easy

You are provided with the following variables:

val x = 24
val y = 7

Using these variables, calculate and print the following on separate lines:

  1. The sum of x and y
  2. The difference when y is subtracted from x
  3. The product of x and y
  4. The result of dividing x by y (integer division)

Remember: Integer division truncates the decimal part, so the last result will be a whole number.

Try it yourself

fun main() {
    val x = 24
    val y = 7
    
    // TODO: Write your code below
    // Calculate and print:
    // 1. The sum of x and y
    // 2. The difference when y is subtracted from x
    // 3. The product of x and y
    // 4. The result of dividing x by y (integer division)
    
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Kotlin playground