Kotlin Program to check a number is even or odd


In this program, You will learn how to check a number is even or odd in Kotlin.


if (num % 2 == 0) { 
     //statement
}

Example: How to check a number is even or odd in Kotlin

import java.util.Scanner

fun main(args: Array<String>) {

    var n: Int

    var sc = Scanner(System.`in`)

    print("Enter a number:")
    n = sc.nextInt()

    if (n % 2 == 0) {
        println("$n is even")
    } else {
        println("$n is odd")
    }
}

Output:

Enter a number:10
10 is even