Golang arithmetic Calculator using switch case example

This blog post covers an example for Arithmetic Simple Calculator using Switch case in Golang.
To understand this example, You should have the following features in the Go language

#Arithmetic Calculator using switch case in Golang examples

The below example is to write a Simple Calculator which supports arithmetic Operators such as Add, Subtract, Multiply, Modulus, Division operations.

First Read the input numbers from the user keyboard console, stored in variables number1 and number2.

And also Asks the user to enter Operator from the console, stores it in the Operator symbol. The user values are read from the console using the `Scanln` function.

For example, If the user entered `number1`=20 and `number2`=10 and `operator`=+

Matched Operator case executed using switch statements and returned the result.

Finally, Print the output to the console using the `Printf` function.

``````package main

import "fmt"

func main() {
var operator string
var number1, number2 int
fmt.Scanln( & number1)
fmt.Scanln( & number2)
fmt.Scanln( & operator)
output: = 0
switch operator {
case "+":
output = number1 + number2
break
case "-":
output = number1 - number2
case "*":
output = number1 * number2
case "/":
output = number1 / number2
case "%":
output = number1 % number2
default:
fmt.Println("Invalid Operation")
}
fmt.Printf("%d %s %d = %d", number1, operator, number2, output)
}

``````

The output of the above program is

``````Please enter First number: 20
20 %!c(string=+) 10 = 30

20 + 10 = 30

10 / 5 = 2

8 % 2 = 0

6 * 2 = 12

10 - 2 = 8
``````

#Conclusion

This is a simple arithmetic calculator using a switch case example in Go Language.