Arithmetic operators
- Kotlin course
- Beginner
- Lesson 24/116
- 4 min
Learn arithmetic with +, -, *, / and %.
Arithmetic operators
In Kotlin there are five basic arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division) and % (modulo).
Integer division yields an integer: 7 / 2 → 3. Modulo yields the remainder: 7 % 2 → 1.
val a = 10
val b = 3
println(a + b) // 13
println(a - b) // 7
println(a * b) // 30
println(a / b) // 3
println(a % b) // 1Practice Try arithmetic
Write a fun main(). Declare val a = 10 and val b = 3. Print the remainder with println(a % b).
Sample solution
fun main() {
val a = 10
val b = 3
println(a % b)
}Expected output
1
Integer division
In Kotlin the division of two Ints yields an Int; so 7 / 2 → 3 (not 3.5). For a decimal result, at least one side must be Double: 7.0 / 2 → 3.5 or 7 / 2.0 → 3.5.
This behavior is a common pitfall when integer division does not yield the expected result. Make both sides Double for consistency.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does `7 % 2` return?
- 0
- 3
- 3.5
- 1 Correct answer
Explanation: The remainder of 7 divided by 2 is 1.
2 Arithmetic operators: fill in the missing part
val remainder = 7 ____ 2
println(remainder) // 1
val remainder = 7 % 2
println(remainder) // 1
Explanation: The % operator yields the remainder.
3 Arithmetic operators: match the terms
| Term | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
% |
Modulo |