Kotlin Program To Check Number is Odd or Even

In this post, we will go through Kotlin program to check if number is odd or even.

How do you check whether number is odd or even?

Answer – When it is divided by 2 and remainder is 0. Then, it is even. Otherwise it’s odd.

Program to Check Odd or Even in Kotlin

Sourcecode –

import java.util.*

fun main(args: Array<String>) {

    val read = Scanner(System.`in`)

    println("Enter integer number:")
    val a = read.nextInt()

    // Logic to check even
    val isEven = ((a % 2) == 0)

    val res = "Entered value is " + (if (isEven) "Even" else "Odd")

    println(res)
}

When you run the program, output will be –

 Enter integer number:
 43
 Entered value is Odd

Logic to check for even number –
((a % 2) == 0) .

Here, % returns remainder when you divide a number by other number.

So, When you enter 43, it is divided by 2 and remainder is checked if it is 0 or not.
Since remainder for 43 will be 1, it’s an odd number.

Thus, we went through program to check if number is odd or even in Kotlin

Leave a Reply