Kotlin Program to Find the Largest Among Three Numbers


In this article, we will understand how to find the largest of three integers in Kotlin. This is done using a greater than operator (<). Here we use a simple if-else condition to compare the values.

Below is a demonstration of the same

Suppose our input is −

-50, 30 and 50

The desired output would be −

The largest number is 50

Algorithm

  • Step 1 − Start

  • Step 2 − Declare three integers: input1, input2 and input3

  • Step 3 − Define the integers

  • Step 4 − Using an if else loop, compare the first input with the other two inputs to check if it is the largest of the three integers. If not, repeat the step for the other two integers.

  • Step 5 − Display the result

  • Step 6 − Stop

Example 1

In this example, we will find the largest among three numbers using the if…elseif…else statement in Kotlin. First, declare and initialize the three numbers

val input1 = -50
val input2 = 30
val input3 = 50

The, find the largest among the above three numbers using if…elseif…else −

if (input1 >= input2 && input1 >= input3)
   println("The first value i.e $input1 is the largest number.")
else if (input2 >= input1 && input2 >= input3)
   println("The second value i.e $input2 is the largest number.")
else
   println("The third value i.e $input3 is the largest number.")

Let us now see the complete example to find the largest among three numbers in Kotlin −

fun main() { val input1 = -50 val input2 = 30 val input3 = 50 println("The values are defined as $input1, $input2 and $input3") if (input1 >= input2 && input1 >= input3) println("The first value i.e $input1 is the largest number.") else if (input2 >= input1 && input2 >= input3) println("The second value i.e $input2 is the largest number.") else println("The third value i.e $input3 is the largest number.") }

Output

The values are defined as -50, 30 and 50
The third value i.e 50 is the largest number.

Example 2

In this example, we will find the largest among three numbers in Kotlin

fun main() { val input1 = -50 val input2 = 30 val input3 = 50 println("The values are defined as $input1, $input2 and $input3") printLargestValue(input1, input2, input3) } fun printLargestValue(input1: Int, input2: Int, input3: Int) { if (input1 >= input2 && input1 >= input3) println("The first value i.e $input1 is the largest number.") else if (input2 >= input1 && input2 >= input3) println("The second value i.e $input2 is the largest number.") else println("The third value i.e $input3 is the largest number.") }

Output

The values are defined as -50, 30 and 50
The third value i.e 50 is the largest number.

Example 3

In this example, we will find the largest among three numbers in Kotlin using the when statement −

fun main() { val input1 = 15 val input2 = 55 val input3 = 35 println("The values are defined as $input1, $input2 and $input3") when { input1 >= input2 && input1 >= input3 -> println("$input1 is the largest number.") input2 >= input1 && input2 >= input3 -> println("$input2 is the largest number.") else -> println("$input3 is the largest number.") } }

Output

The values are defined as 15, 55 and 35
55 is the largest number.

Updated on: 13-Oct-2022

740 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements