Kotlin – Check if Two Numbers are Equal

Check if two numbers are equal in Kotlin

In Kotlin, you can check if given two numbers are equal using Equal-to Operator.

For example, if a and b are the given two numbers, then the expression to check if these two numbers are equal is

a == b

We can use this as a condition in if-else statement.

Examples

Check if 20 and 20 are equal numbers

In the following example, we take two numbers in a and b, say 20 and 20 respectively, and programmatically check if these two numbers are equal.

Kotlin Program

fun main() {
    val a = 20
    val b = 20

    if (a == b) {
        println("$a and $b are equal numbers.")
    } else {
        println("$a and $b are not equal numbers.")
    }
}

Output

20 and 20 are equal numbers.

Check if 35 and 84 are equal numbers

In the following example, we take two numbers in a and b, say 35 and 84 respectively, and programmatically check if these two numbers are equal.

Kotlin Program

fun main() {
    val a = 35
    val b = 84

    if (a == b) {
        println("$a and $b are equal numbers.")
    } else {
        println("$a and $b are not equal numbers.")
    }
}

Output

35 and 84 are not equal numbers.