Kotlin Modulus Assignment Operator
Kotlin Modulus Assignment Operator assigns the left operand with remainder in the division of the left operand by the right operand.
Syntax
The syntax to use Modulus Assignment operator is
x %= value
where
x
is the variable name%=
is the symbol used for Modulus Assignment operatorvalue
is the value that we would like to divide the variablex
with, and assignx
with the remainder of the division
The above expression is equivalent to
x = x % value
Examples
Assign x with the remainder in the divine of x by 5
In the following program, we take a variable: x
with an initial value of 21
. Using Modulus Assignment operator, we divide the value in x
by 5
and assign the remainder to x
.
Kotlin Program
fun main() {
var x = 21
x %= 5
println(x)
}
Output
1
Explanation
x %= 5
x = x % 5
x = 21 % 5
x = 1