In this tutorial, you shall learn how to find the smallest of given two numbers in Kotlin, with the help of example programs.

Kotlin – Find smallest of two numbers

To find the smallest of given two numbers in Kotlin, we can use Greater-than or Less-than operator and compare the two numbers.

If a and b are given two numbers, the condition to check if a is less than b is

</>
Copy
a < b

The above condition returns true if a is less than b, or else, it returns false.

Program

In the following program, we read two numbers from user and store them in a and b, then use Less-than comparison operator to find the smallest of a and b.

Main.kt

</>
Copy
fun main() {
    print("Enter first number : ")
    val a = readLine()!!.toDouble()
    print("Enter second number : ")
    val b = readLine()!!.toDouble()

    if (a < b) {
        print("$a is the smallest of $a and $b")
    } else {
        print("$b is the smallest of $a and $b")
    }
}

Output #1

Enter first number : 4
Enter second number : 9
4.0 is the smallest of 4.0 and 9.0

Output #2

Enter first number : 8
Enter second number : 5
5.0 is the smallest of 8.0 and 5.0

Related tutorials for the above program

Conclusion

In this Kotlin Tutorial, we learned how to find the smallest of given two numbers.