In this tutorial, you shall learn how to write a program to check if given number is an even number in Kotlin.
Kotlin – Even Number Program
To check if given number is even or not in Kotlin, divide the given number by 2 and check the remainder. If the remainder is zero, then the given number is an even number, else not.
If n
is the given number, then the condition to check if n
is even or not is
n % 2 == 0
The above expression returns true if n
is even, or else it returns false.
Program
In the following program, we take a number in n
, and check if it is even number or not using the above condition.
Main.kt
fun main() { print("Enter an integer : ") val n = readLine()!!.toInt() if (n % 2 == 0) { println("$n is even.") } else { println("$n is not even.") } }
Output #1
Enter an integer : 8 8 is even.
Output #2
Enter an integer : 5 5 is not even.
ADVERTISEMENT
Related Tutorials
Conclusion
In this Kotlin Tutorial, we learned how to check if given number is even number or not.