Tuesday, March 5, 2024

Program 5 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 5- -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers. 

Source Code



# Function to perform addition
addition() {
    echo "$num1 + $num2" | bc
}

# Function to perform subtraction
subtraction() {
    echo "$num1 - $num2" | bc
}

# Function to perform multiplication
multiplication() {
    echo "$num1 * $num2" | bc
}

# Function to perform division
division() {
    if [ "$num2" -eq 0 ]; then
        echo "Error: Division by zero!"
    else
        echo "scale=2; $num1 / $num2" | bc
    fi
}

# Main script starts here

# Prompt the user to enter the first number
echo "Enter the first number: "
    read num1

    # Prompt the user to enter the second number
    echo "Enter the second number: "
    read num2
       
while true; do
    # Prompt the user to select the operation
    echo "Select the operation:"
    echo "1. Addition"
    echo "2. Subtraction"
    echo "3. Multiplication"
    echo "4. Division"
    echo "5. Exit"
    echo "Enter your choice: "
    read choice

       
    case $choice in
        1) echo "Addition: $(addition)";;
        2) echo "Subtraction: $(subtraction)";;
        3) echo "Multiplication: $(multiplication)";;
        4) echo "Division: $(division)";;
        5) echo "Exiting..."; exit;;
        *) echo "Invalid choice";;
    esac
done


SAMPLE OUTPUT:




No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...