-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.sh
More file actions
49 lines (47 loc) · 1.17 KB
/
calculator.sh
File metadata and controls
49 lines (47 loc) · 1.17 KB
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
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
while true; do
echo "Simple Calculator"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Exit"
read -p "Select an option (1-5): " choice
case "$choice" in
1)
read -p "Enter first number: " num1
read -p "Enter second number: " num2
result=$((num1 + num2))
echo "Result: $result"
;;
2)
read -p "Enter first number: " num1
read -p "Enter second number: " num2
result=$((num1 - num2))
echo "Result: $result"
;;
3)
read -p "Enter first number: " num1
read -p "Enter second number: " num2
result=$((num1 * num2))
echo "Result: $result"
;;
4)
read -p "Enter dividend: " dividend
read -p "Enter divisor: " divisor
if [ "$divisor" -eq 0 ]; then
echo "Error: Division by zero!"
else
result=$(awk "BEGIN {printf \"%.2f\", $dividend / $divisor}")
echo "Result: $result"
fi
;;
5)
echo "Goodbye!"
exit 0
;;
*)
echo "Invalid option. Please choose a valid option (1-5)."
;;
esac
done