Kotlin – Smallest of Three Numbers

How to Find Smallest Number of Three Numbers in Kotlin

In Kotlin, finding the smallest of three numbers involves comparing the numbers and identifying the smallest value among them. We will use Kotlin `minOf()` function to find the smallest number.

Examples

Example 1: Find the Smallest of Three Numbers

Let’s start with an example of finding the smallest of three numbers.

We’ll create a function that takes three integers as input and returns the smallest value among them.

Kotlin Program

``````fun findSmallest(num1: Int, num2: Int, num3: Int): Int {
return minOf(num1, num2, num3)
}

fun main() {
// Test the function with sample numbers
val num1 = 10
val num2 = 5
val num3 = 8
val smallest = findSmallest(num1, num2, num3)
println("The smallest of \$num1, \$num2, and \$num3 is: \$smallest")
}``````

Output

``The smallest of 10, 5, and 8 is: 5``

In the function,

• We define a function `findSmallest` that takes three integers as input: `num1`, `num2`, and `num3`.
• Inside this function, we use the `minOf` function to find the smallest value among the three numbers.
• The `minOf` function automatically compares the provided arguments and returns the smallest value.

Summary

In Kotlin, you can find the smallest of three numbers by using the `minOf()` function, which returns the smallest value among the provided arguments.