Lesson 4 of 17

Numbers

Numbers in Kotlin

Kotlin's two main numeric types are Int (whole numbers) and Double (decimals).

val x = 10       // Int
val pi = 3.14    // Double

Arithmetic operators work as expected:

println(10 + 3)   // 13
println(10 - 3)   // 7
println(10 * 3)   // 30
println(10 % 3)   // 1  (remainder)

Useful math functions:

kotlin.math.sqrt(16.0)  // 4.0
kotlin.math.abs(-5)     // 5
minOf(3, 7)             // 3
maxOf(3, 7)             // 7

Your Turn

Compute and print: the sum, difference, product, and remainder when dividing 15 by 4.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.