Kotlin Program to Create Simple Calculator
when to perform addition, subtraction, multiplication, or division, like when(operator) { "+" -> num1 + num2; "-" -> num1 - num2; "*" -> num1 * num2; "/" -> num1 / num2 }.Examples
How to Think About It
when. Perform the matching math operation. Handle division carefully to avoid dividing by zero. Finally, show the result.Algorithm
Code
fun main() {
print("Enter first number: ")
val num1 = readLine()!!.toDouble()
print("Enter second number: ")
val num2 = readLine()!!.toDouble()
print("Enter operator (+, -, *, /): ")
val operator = readLine()!!
val result = when (operator) {
"+" -> num1 + num2
"-" -> num1 - num2
"*" -> num1 * num2
"/" -> if (num2 != 0.0) num1 / num2 else null
else -> null
}
if (result != null) {
println("Result: $result")
} else {
println("Cannot divide by zero or invalid operator")
}
}Dry Run
Let's trace the input num1=5, num2=3, operator='+' through the code
Read first number
num1 = 5.0
Read second number
num2 = 3.0
Read operator
operator = '+'
Check operator and calculate
operator is '+', so result = 5.0 + 3.0 = 8.0
Print result
Output: Result: 8.0
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 5.0 |
| 2 | num2 | 3.0 |
| 3 | operator | + |
| 4 | result | 8.0 |
Why This Works
Step 1: Reading inputs
The program uses readLine() to get user input as strings and converts numbers to Double for calculations.
Step 2: Choosing operation
The when expression selects the math operation based on the operator entered.
Step 3: Handling division
Before dividing, the program checks if the divisor is zero to avoid errors and returns null if invalid.
Step 4: Displaying result
If the result is valid, it prints it; otherwise, it shows an error message.
Alternative Approaches
fun main() {
print("Enter first number: ")
val num1 = readLine()!!.toDouble()
print("Enter second number: ")
val num2 = readLine()!!.toDouble()
print("Enter operator (+, -, *, /): ")
val operator = readLine()!!
val result = if (operator == "+") {
num1 + num2
} else if (operator == "-") {
num1 - num2
} else if (operator == "*") {
num1 * num2
} else if (operator == "/") {
if (num2 != 0.0) num1 / num2 else null
} else null
if (result != null) {
println("Result: $result")
} else {
println("Cannot divide by zero or invalid operator")
}
}fun add(a: Double, b: Double) = a + b fun subtract(a: Double, b: Double) = a - b fun multiply(a: Double, b: Double) = a * b fun divide(a: Double, b: Double) = if (b != 0.0) a / b else null fun main() { print("Enter first number: ") val num1 = readLine()!!.toDouble() print("Enter second number: ") val num2 = readLine()!!.toDouble() print("Enter operator (+, -, *, /): ") val operator = readLine()!! val result = when (operator) { "+" -> add(num1, num2) "-" -> subtract(num1, num2) "*" -> multiply(num1, num2) "/" -> divide(num1, num2) else -> null } if (result != null) { println("Result: $result") } else { println("Cannot divide by zero or invalid operator") } }
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables to store inputs and results, so space usage is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; using when is more concise and readable than if-else chains.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using when | O(1) | O(1) | Simple, readable code |
| Using if-else | O(1) | O(1) | Familiar to beginners but less concise |
| Using functions | O(1) | O(1) | Better structure for larger calculators |