Even Number Program in Kotlin
In Kotlin, you can check if given number is an even number or not, by dividing the given number with 2, and checking if the remainder is zero or not.
To find the remainder, we can use Arithmetic Modulus Operator, and to check if the remainder is zero, we can use Equal-to Operator.
For example, if n
is the given number, then the boolean expression to check if n
is an even number is
n % 2 == 0
We can use this as a condition in if-else statement.
Examples
1. Check if 20 is even number in Kotlin
In the following example, we take a number in n
, say 20, and check if this is an even number or not.
Kotlin Program
fun main() {
val n = 20
if (n % 2 == 0) {
println("$n is even.")
} else {
println("$n is not even.")
}
}
Output
20 is even.
2. Check if 35 is even number in Kotlin
In the following example, we take a number in n
, say 35, and check if this is an even number or not.
Kotlin Program
fun main() {
val n = 35
if (n % 2 == 0) {
println("$n is even.")
} else {
println("$n is not even.")
}
}
Output
35 is not even.