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), and divide(a, b).

  • Mathematical Functions: log10(x), sine(x), and cosine(x) use the math 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.


Was this helpful?

India's 1st AI Doubt Solver for CBSE, JEE, and NEET

Ask a Question for Free

and then it's just ₹212 a month

Learn how Chatterbot AI can help you succeed

Cover Image of the article Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

This ultimate guide for CBSE Computer Science class 11 has detailed notes, NCERT solutions, cheat sheets, and our free AI-powered doubt-solving assistant, Chatterbot AI.

Cover Image of the article JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 on May 26! Get exam schedule, syllabus, prep tips & more in this guide. Ace India's top engineering test with topper strategies.

Cover Image of the article How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

Ace NEET with expert strategies: Discover effective exam strategies, time management, core concepts mastery, problem-solving techniques, revision tips, and AI-assisted doubt clearing with Chatterbot AI.

Cover Image of the article How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

Ace IIT JEE in 6 months without coaching. Discover expert self-study strategies for Physics, Chemistry, and Math. Master time management, mock tests, and leverage AI tools like Chatterbot AI for personalized doubt-solving.