- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Java Program to Find the Largest Among Three Numbers
- Swift Program to Find the Largest Among Three Numbers
- Haskell program to find the largest among three numbers
- C program to Find the Largest Number Among Three Numbers
- C++ Program to Find Largest Number Among Three Numbers
- How to Find the Largest Among Three Numbers in the Golang program?
- Java program to Largest number among three numbers
- How to Find the Largest Among Three Numbers using Python?
- Program to find largest of three numbers - JavaScript
- Java program to find largest of the three numbers using ternary operators
- C++ program to find the smallest element among three elements
- Kotlin Program to Find GCD of two Numbers
- Kotlin Program to Find LCM of two Numbers
- C# program to find the maximum of three numbers
- Python program to find the maximum of three numbers
