Write a Kotlin Program to Make a Simple Calculator Using switch…case

In this Kotlin programming tutorial, we will create a simple calculator using the switch…case statement.

The switch…case statement is a useful control flow statement that allows you to execute different code blocks based on the value of a variable.

Before we dive into the coding, let’s discuss the approach we will take to build our calculator.

Our calculator will accept two numbers and an operation, such as addition, subtraction, multiplication, or division.

The program will then perform the requested operation and display the result.

Let’s begin by creating a main function in our Kotlin program.

This function will prompt the user for two numbers and an operation, and then call a calculate function to perform the operation and display the result.

Here is the code for our main function:

fun main() {
    println("Enter first number:")
    val num1 = readLine()?.toDouble()

    println("Enter second number:")
    val num2 = readLine()?.toDouble()

    println("Enter operation (+, -, *, /):")
    val operation = readLine()

    calculate(num1, num2, operation)
}

In this code, we use the readLine() function to prompt the user for input and store the results in the num1, num2, and operation variables.

We also use the safe call operator ?. to ensure that the input can be converted to a Double type without causing any null pointer exceptions.

Now, let’s create the calculate function that will perform the requested operation.

We will use a switch…case statement to determine which operation to perform. Here is the code for our calculate function:

fun calculate(num1: Double?, num2: Double?, operation: String?) {
    when(operation) {
        "+" -> println("$num1 + $num2 = ${num1?.plus(num2!!)}")
        "-" -> println("$num1 - $num2 = ${num1?.minus(num2!!)}")
        "*" -> println("$num1 * $num2 = ${num1?.times(num2!!)}")
        "/" -> println("$num1 / $num2 = ${num1?.div(num2!!)}")
        else -> println("Invalid operation")
    }
}

In this code, we use the when keyword to evaluate the value of the operation variable.

Depending on the value of the variable, we perform the corresponding operation using the Double extension functions plus(), minus(), times(), or div().

We also use the Elvis operator ?. to handle null values in the num1 and num2 variables.

Finally, we print the result of the operation to the console using string interpolation.

That’s it! We have created a simple calculator using the switch…case statement in Kotlin.

Here is the complete code for reference:

fun main() {
    println("Enter first number:")
    val num1 = readLine()?.toDouble()

    println("Enter second number:")
    val num2 = readLine()?.toDouble()

    println("Enter operation (+, -, *, /):")
    val operation = readLine()

    calculate(num1, num2, operation)
}

fun calculate(num1: Double?, num2: Double?, operation: String?) {
    when(operation) {
        "+" -> println("$num1 + $num2 = ${num1?.plus(num2!!)}")
        "-" -> println("$num1 - $num2 = ${num1?.minus(num2!!)}")
        "*" -> println("$num1 * $num2 = ${num1?.times(num2!!)}")
        "/" -> println("$num1 / $num2 = ${num1?.div(num2!!)}")
        else -> println("Invalid operation")
}

To run the program, open your Kotlin IDE or any text editor that supports Kotlin programming, and copy the above code into it.

Save the code with a .kt extension and then compile and run it.

Upon execution, the program will prompt you to enter the first and second number, followed by the operation.

After entering the values, the program will perform the operation and display the result on the console.

In conclusion, the switch…case statement is a useful control flow statement that allows you to execute different code blocks based on the value of a variable.

We have used this statement in our Kotlin program to create a simple calculator that performs basic arithmetic operations.

This program is a good starting point for beginners who want to learn how to use the switch…case statement in Kotlin programming.