Golang Program to make a Simple Calculator using Switch Case


In this tutorial we will see how to make a simple calculator using switch case statements in Go programming language.

The switch statement will assess an expression, comparing the expression's value against a series of case conditions given in a series and executes the statement after the first condition with a matching value, until a break is encountered.

Golang Basic switch case with default

  • A switch statement runs the first case equal to the choice entered.

  • The cases are evaluated in an order and it stops when a case succeeds.

  • If no case (choice entered) matches, it is a default case and its statements are executed.

How to make a simple calculator using switch case in

Syntax

switch expression {
   case 1:
   // code block 1
	
   case 2:
   // code block 2
	
   ...
   ...
   default:
   // default code block
}

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Start function main()

  • Step 3 − Declare and initialize the variables

  • Step 4 − Create the switch case statement

  • Step 5 − Print the result using built-in function fmt.Println()

Example

Shows how to make a simple calculator using switch case in golang program

// Golang program to make a Simple // Calculator using Switch Case package main // fmt package provides the function to print anything import "fmt" // start the main() function func main() { // Declare amd initialize the variables var number1 int=20 var number2 int=10 var choice int = 0 // choice of the input calculation var x int // the result variable fmt.Println("number 1 = ",number1,"\nnumber 2 =",number2) fmt.Println(" choice 1: Addition of the two numbers") fmt.Println(" choice 2: Subtraction of the two numbers") fmt.Println(" choice 3: Multiplication of the two numbers") fmt.Println(" choice 4: Division of the two numbers") fmt.Scanln(&choice) // print the choice of calculation using switch case switch choice{ case 1: x=number1+number2 fmt.Printf("Addition of the two numbers is: %d",x) case 2: x=number1-number2 fmt.Printf("Subtraction of the two numbers is: %d",x) case 3: x=number1*number2 fmt.Printf("Multiplication of the two numbers is: %d",x) case 4: x=number1/number2 fmt.Printf("Division of the two numbers is: %d",x) default: fmt.Println("Invalid number") } // Print the result using built-in function fmt.Println() }

Input

number 1 = 20
number 2 = 10
choice 1: Addition of the two numbers
choice 2: Subtraction of the two numbers
choice 3: Multiplication of the two numbers
choice 4: Division of the two numbers
2

Output

Subtraction of the two numbers is: 10

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main().GO program execution starts with the function main().

  • Declare and initialize the variables number1 and number2, variable choice corresponds to the choice of calculation. Variable x is the result integer variable.

  • Create the switch case statement to execute the code

  • Last we print the result on the screen using built-in function fmt.Println().This function is defined under the fmt package and it helps to write standard output.

How to Make a Simple Calculator using Switch Case in two Separate Functions

Syntax

func functionname(list_of_parameters)(return_type) {
   //...
   //function_body
}

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Create the function calculator()

  • Step 3 − Declare and initialize the variables

  • Step 4 − Create the switch case statement

  • Step 5 − Start the function main()

  • Step 6 − Call the function calculator()

  • Step 7 − Print the result using built-in function fmt.Println().

Example

Shows how to make a simple calculator using switch case in golang program in two separate functions

// Golang program to make a Simple // Calculator using Switch Case package main // fmt package provides the function to print anything import "fmt" // Creating a function Calculator() func calculator(choice int) int { // declare and initialize the variables var result int var num1 int = 30 var num2 int = 15 // print the choice of calculation using switch case switch choice { case 1: result = num1 + num2 fmt.Printf("Addition is: %d \n", result) case 2: result = num1 - num2 fmt.Printf("Subtraction is: %d \n", result) case 3: result = num1 * num2 fmt.Printf("Multiplication is: %d \n", result) case 4: result = num1 / num2 fmt.Printf("Division is: %d \n", result) default: fmt.Println("Invalid value") } return 0 } // start the main() function func main() { fmt.Println("Number 1 = 30 \nNumber 2= 15") fmt.Println("Enter the following operation you want to perform") fmt.Println("1 for addition \n2 for Subtration \n3 for Multiplication \n4 for Division") var option int = 0 // calling the calculator() function fmt.Scanln(&option) calculator(option) // Print the result using built-in function fmt.Println() }

input

Number 1 = 30
Number 2= 15
Enter the following operation you want to perform
1 for addition
2 for Subtration
3 for Multiplication
4 for Division
2

Output

Subtraction is: 15

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • Create the function calculator() to calculate the choice

  • Declare and initialize the variables num1 and num2. Variable result is the final result integer variable.

  • Create the switch case statement to execute the code with the input choice

  • Next we start the function main().GO program execution starts with the function main().

  • Here we will use user input function – fmt.Scanln(), and then we call the function calculator() to calculate the result

  • The final result is printed on the console screen using the built-in function fmt.Println().This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above two examples we have successfully compiled and executed the Golang code to make a simple calculator using switch case.

Even though we can use if...else statements in the place of switch case statements, but the code written with the switch case is much cleaner and easier to write.

Updated on: 28-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements