# Golang Example - Program Simple arithmetic Calculator Using switch...case

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

### Example program - Arthematic CalculatorĀ  using switch case

The below example is to write Simple Calculator which supports Arthematic Operators like Add, Subtract, Multiply, Modulus, DivisionĀ

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

And also Asks the user to enter Operator from the console, stores it in Operator symbol. The user values read from the console using 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 console usingĀ  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

``````
Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts