Kotlin – Find maximum of two numbers

Find Maximum of Two Numbers in Kotlin

In this tutorial, you’ll learn how to find the maximum of two numbers using Kotlin.

Finding the maximum of two numbers is a common task in programming. In Kotlin, you can achieve this by using a simple comparison between the given two numbers, and using this comparison expression as a condition in the if-else construct.

To compare the given two numbers, we can use Greater-than comparison operator.


Finding Maximum of Two Numbers

We’ll start with an example of finding the maximum of two numbers.

In this example, the findMaximum() function compares two numbers and returns the greater of the two.

Kotlin Program

// Function to find maximum of two numbers
fun findMaximum(a: Int, b: Int): Int {
    if (a > b) {
        return a
    } else {
        return b

fun main() {
    val number1 = 8
    val number2 = 12
    val maximum = findMaximum(number1, number2)
    println("Maximum of $number1 and $number2 is: $maximum")


Maximum of 8 and 12 is: 12


In this Kotlin Tutorial, we learned how to find the maximum of given two numbers in Kotlin, using greater than comparison operator, with the help of example programs.