How to find the Maximum of Two Numbers in Kotlin


How to find the Maximum of Two Numbers in Kotlin ?

Answer

To find the maximum of two numbers in Kotlin, you can use the coerceAtLeast function.



✐ Examples

1 Maximum of Two Numbers

In this example,

  1. We declare two variables a and b and assign values to them.
  2. We use the coerceAtLeast function to find the maximum of a and b and store it in a variable named max.
  3. We print the maximum value using println.

Kotlin Program

fun main() {
    val a = 10
    val b = 15
    val max = a.coerceAtLeast(b)
    println("Maximum of $a and $b is: $max")
}

Output

Maximum of 10 and 15 is: 15

Summary

In this tutorial, we learned How to find the Maximum of Two Numbers in Kotlin language with well detailed examples.




More Kotlin Comparison Operations Tutorials

  1. How to find the Maximum of Two Numbers in Kotlin ?
  2. How to find the Minimum of Two Numbers in Kotlin ?
  3. How to find the Maximum of Three Numbers in Kotlin ?
  4. How to find the Minimum of Three Numbers in Kotlin ?