Write a shell script to implement a menu driven calculator with following functionsa) Additionb) Subtractionc) Multiplicationd) Divisione) Modulus
Question
Write a shell script to implement a menu driven calculator with following functions
a) Addition
b) Subtraction
c) Multiplication
d) Division
e) Modulus
Solution
Sure, here is a simple shell script for a menu driven calculator:
#!/bin/bash
while true
do
echo "Menu Driven Calculator"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Modulus"
echo "6. Exit"
echo "Enter your choice: "
read choice
if [ $choice -eq 6 ]
then
break
fi
echo "Enter two numbers: "
read num1 num2
case $choice in
1) result=`echo $num1 + $num2 | bc`
;;
2) result=`echo $num1 - $num2 | bc`
;;
3) result=`echo $num1 \* $num2 | bc`
;;
4) result=`echo "scale=2; $num1 / $num2" | bc`
;;
5) result=`echo $num1 % $num2 | bc`
;;
*) echo "Invalid option"
;;
esac
echo "Result : $result"
done
This script uses a while loop to keep the calculator running until the user chooses to exit. It uses the read
command to get user input for the operation choice and the two numbers. It then uses a case statement to perform the chosen operation. The bc
command is used to perform the arithmetic. The scale=2
option in the division operation is used to get the result up to 2 decimal places.
Similar Questions
C Program to Calculate Multiplication and Addition, and Check Division Remainder
Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function
Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
In subtraction operation, which operation is performed if X-OR result of minuend and subtrahend is 1*SubtractionAdditionDivisionMultiplication
Which of the following pre-computer age of 19th century is use for addition and subtraction*1 pointpascal's calculatorside ruleabacusprjector
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.