Kotlin Program to Find Largest of Three Numbers
You can find the largest of three numbers in Kotlin using
if-else statements like this: val largest = if (a >= b && a >= c) a else if (b >= a && b >= c) b else c.Examples
Inputa=3, b=7, c=5
Output7
Inputa=10, b=10, c=2
Output10
Inputa=-1, b=-5, c=-3
Output-1
How to Think About It
To find the largest of three numbers, compare the first number with the other two using
&& (and) operator. If it is greater or equal to both, it is the largest. Otherwise, check the second number similarly. If neither the first nor second is largest, the third must be the largest.Algorithm
1
Get three numbers as input: a, b, and c.2
Check if a is greater than or equal to both b and c.3
If yes, a is the largest number.4
Otherwise, check if b is greater than or equal to both a and c.5
If yes, b is the largest number.6
Otherwise, c is the largest number.7
Print the largest number.Code
kotlin
fun main() {
val a = 3
val b = 7
val c = 5
val largest = if (a >= b && a >= c) a else if (b >= a && b >= c) b else c
println("Largest number is: $largest")
}Output
Largest number is: 7
Dry Run
Let's trace the example where a=3, b=7, c=5 through the code.
1
Compare a with b and c
Check if 3 >= 7 and 3 >= 5 → false
2
Compare b with a and c
Check if 7 >= 3 and 7 >= 5 → true
3
Assign largest
largest = 7
4
Print result
Output: Largest number is: 7
| Step | Condition | Result | Largest |
|---|---|---|---|
| 1 | 3 >= 7 && 3 >= 5 | false | - |
| 2 | 7 >= 3 && 7 >= 5 | true | 7 |
| 3 | Assign largest | - | 7 |
Why This Works
Step 1: Check first number
We use if to check if the first number is greater than or equal to the other two numbers.
Step 2: Check second number
If the first is not largest, we check if the second number is greater than or equal to the other two.
Step 3: Default to third number
If neither first nor second is largest, the third number must be the largest.
Alternative Approaches
Using maxOf function
kotlin
fun main() {
val a = 3
val b = 7
val c = 5
val largest = maxOf(a, b, c)
println("Largest number is: $largest")
}This uses Kotlin's built-in maxOf function for cleaner and more readable code.
Using when expression
kotlin
fun main() {
val a = 3
val b = 7
val c = 5
val largest = when {
a >= b && a >= c -> a
b >= a && b >= c -> b
else -> c
}
println("Largest number is: $largest")
}This uses Kotlin's when expression as an alternative to if-else for clarity.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of comparisons (at most two), so it runs in constant time O(1).
Space Complexity
Only a few variables are used, so the space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in constant time, but using maxOf is more readable and idiomatic in Kotlin.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else statements | O(1) | O(1) | Simple logic, beginner understanding |
| maxOf function | O(1) | O(1) | Concise and idiomatic Kotlin code |
| when expression | O(1) | O(1) | Clear alternative to if-else |
Use Kotlin's built-in
maxOf function for a concise way to find the largest number.Beginners often forget to use >= instead of >, which can cause incorrect results when numbers are equal.