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