Quotient

Report a typo

The code provided below attempts to take two Int numbers as input from the user and calculate their quotient. However, it contains several errors that prevent it from working correctly. Your task is to fix the code so that it properly handles exceptions and calculates the quotient if both inputs are valid integers.

Sample Input 1:

5
0

Sample Output 1:

Cannot divide by zero!

Sample Input 2:

a
5

Sample Output 2:

Numerator and denominator must be valid integers.
Write a program in Kotlin
fun main() {
val numerator = readln()
val denominator = readln()
try {
val result = numerator.toInt() / denominator.toInt()
println(result)
} catch (e: ArrayIndexOutOfBoundsException) {
println("Numerator and denominator must be valid integers.")
} catch (e: IllegalArgumentException) {
println("Cannot divide by zero!")
} catch (e: Exception) {
println(e.message)
}
}

Create a free account to access the full topic