Computer Science
Create a menu-driven program using user-defined functions to implement a calculator that performs the following:
(a) Basic arithmetic operations (+, −, *, /)
(b) log10(x), sin(x), cos(x)
Python
Python Modules
3 Likes
Answer
import math
def arithmetic_operations():
print("Select operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4':
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid input")
def mathematical_functions():
print("Select function:")
print("1. Logarithm base 10 (log10)")
print("2. Sine (sin)")
print("3. Cosine (cos)")
choice = input("Enter choice (1/2/3): ")
if choice == '1':
num = float(input("Enter a number for log10: "))
if num > 0:
print("Result:", math.log10(num))
else:
print("Error: Logarithm is defined for positive numbers only.")
elif choice == '2':
angle = float(input("Enter the angle in degrees: "))
radians = math.radians(angle)
print("Result:", math.sin(radians))
elif choice == '3':
angle = float(input("Enter the angle in degrees: "))
radians = math.radians(angle)
print("Result:", math.cos(radians))
else:
print("Invalid input")
while True:
print("\nCalculator Menu:")
print("1. Basic Arithmetic Operations")
print("2. Mathematical Functions")
print("3. Exit")
choice = input("Enter choice (1/2/3): ")
if choice == '1':
arithmetic_operations()
elif choice == '2':
mathematical_functions()
elif choice == '3':
print("Exiting the program.")
break
else:
print("Invalid input. Please choose again.")Output
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 1
Select operation:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
Enter choice (1/2/3/4): 2
Enter first number: 34
Enter second number: 7
Result: 27.0
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 2
Select function:
1. Logarithm base 10 (log10)
2. Sine (sin)
3. Cosine (cos)
Enter choice (1/2/3): 1
Enter a number for log10: 24
Result: 1.380211241711606
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 3
Exiting the program.
Answered By
3 Likes
Related Questions
Define a function which accepts n as an argument and prints Fibonacci series till n.
Consider the temperature given below for the month of June in North India. Calculate the average temperature and median value. This temperature gets dipped with a variation of 20°C in the month of December. Write a Python program to calculate the changed median value and average temperature.
Location Temperature (in °C) Delhi 41 Shimla 32 Chandigarh 43 Rohtak 40 Srinagar 28 Sri Ganganagar 45 Create a menu-driven program using user-defined functions to implement a calculator that performs the following:
(a) Basic arithmetic operations (+, −, *, /)
(b) log10(x), sin(x), cos(x)
Write a program that contains user-defined functions to calculate area, perimeter or surface area whichever is applicable, for various shapes like square, rectangle, triangle, circle and cylinder. The user-defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use appropriate functions.