-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalc.go
More file actions
39 lines (33 loc) · 684 Bytes
/
Copy pathcalc.go
File metadata and controls
39 lines (33 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"os"
)
func main() {
var num1, num2 float64
var operator string
fmt.Print("Enter first number: ")
fmt.Scanln(&num1)
fmt.Print("Enter operator (+, -, *, /): ")
fmt.Scanln(&operator)
fmt.Print("Enter second number: ")
fmt.Scanln(&num2)
switch operator {
case "+":
fmt.Printf("Result: %.2f\n", num1+num2)
case "-":
fmt.Printf("Result: %.2f\n", num1-num2)
case "*":
fmt.Printf("Result: %.2f\n", num1*num2)
case "/":
if num2 != 0 {
fmt.Printf("Result: %.2f\n", num1/num2)
} else {
fmt.Println("Error: Division by zero is not allowed.")
os.Exit(1)
}
default:
fmt.Println("Error: Invalid operator.")
os.Exit(1)
}
}