Write a Kotlin Program to Find the Largest Among Three Numbers

In Kotlin, finding the largest among three numbers can be achieved through simple comparisons.

We can take three numbers as input, and then compare them to find the largest one.

Here’s a Kotlin program that demonstrates this:

fun main() {
    val a = 10
    val b = 20
    val c = 30

    var largest = a

    if (b > largest) {
        largest = b
    }
    if (c > largest) {
        largest = c
    }

    println("The largest number is $largest")
}

In this program, we first declare and initialize the three numbers a, b, and c.

We then declare a variable called largest and initialize it to a.

We then compare b and c to largest, and if they are greater, we update the value of largest.

Finally, we print the value of largest using println() function.

This program will output The largest number is 30, which is the expected output as c is the largest number among the three.

We can also modify this program to take input from the user instead of hardcoding the values of a, b, and c. Here’s the modified program:

fun main() {
    print("Enter the first number: ")
    val a = readLine()!!.toInt()

    print("Enter the second number: ")
    val b = readLine()!!.toInt()

    print("Enter the third number: ")
    val c = readLine()!!.toInt()

    var largest = a

    if (b > largest) {
        largest = b
    }
    if (c > largest) {
        largest = c
    }

    println("The largest number is $largest")
}

In this program, we use the readLine() function to take input from the user.

We first prompt the user to enter the first number, and then we read the input and convert it to an integer using the toInt() function.

We repeat this for the second and third numbers.

We then compare the three numbers and find the largest one using the same logic as before.

Finally, we print the largest number using println() function.

This program will prompt the user to enter three numbers, and then it will output the largest one.

In conclusion, finding the largest among three numbers in Kotlin is a simple task that can be accomplished through simple comparisons.

We can take input from the user or use hardcoded values, depending on our requirements.