Create a menu driven program using user defined functions to implement a calculator that performs the following: a) Basic arithmetic operations(+,-,*,/) b) log base 10 (x), sin(x), cos(x)
Question
Create a menu driven program using user defined functions to implement a calculator that performs the following:
a) Basic arithmetic operations(+,-,*,/)
b) $\log _{10}(\mathrm{x}), \sin (\mathrm{x}), \cos (\mathrm{x})$
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here’s a basic menu-driven calculator program implemented using user-defined functions in Python:
import math
# Function to add two numbers
def add(a, b):
return a + b
# Function to subtract two numbers
def subtract(a, b):
return a - b
# Function to multiply two numbers
def multiply(a, b):
return a * b
# Function to divide two numbers
def divide(a, b):
if b == 0:
return "Division by zero is undefined"
return a / b
# Function to calculate log base 10
def log10(x):
return math.log10(x)
# Function to calculate sine of an angle in radians
def sine(x):
return math.sin(x)
# Function to calculate cosine of an angle in radians
def cosine(x):
return math.cos(x)
# Menu driven program
def calculator():
while True:
print("\nMenu:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. log10")
print("6. sin")
print("7. cos")
print("8. Exit")
choice = int(input("Select operation: "))
if choice == 8:
print("Exiting...")
break
if choice in [1, 2, 3, 4]:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == 1:
print("Result: ", add(a, b))
elif choice == 2:
print("Result: ", subtract(a, b))
elif choice == 3:
print("Result: ", multiply(a, b))
elif choice == 4:
print("Result: ", divide(a, b))
elif choice in [5, 6, 7]:
x = float(input("Enter the number/angle in radians: "))
if choice == 5:
print("Result: ", log10(x))
elif choice == 6:
print("Result: ", sine(x))
elif choice == 7:
print("Result: ", cosine(x))
else:
print("Invalid choice, please try again.")
# Run the calculator program
calculator()
Explanation:
Basic Arithmetic Functions:
add(a, b)
,subtract(a, b)
,multiply(a, b)
, anddivide(a, b)
.Mathematical Functions:
log10(x)
,sine(x)
, andcosine(x)
use themath
module to perform these operations.The Menu Driven Program interacts with the user to select an operation, takes input numbers, performs the operations, and displays results.
The program will run in a loop until the user selects the option to exit.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month