Page 626 - ComputerScience_Class_11
P. 626

The output of the preceding program is as follows:

                   Output

                Enter the first number: 10
                Enter the second number: 10
                Enter the operation (+, -, *, /): *
                Result:  100.0


              34.  Write a menu-driven program to calculate the area of a circle or the area of a rectangle or check if a number is
                 prime or not based on the user's choice.

                   Program 34.py
                File  Edit  Format   Run   Options   Window    Help


                import math
                def calculate_rectangle_area():
                    length = float(input("Enter the length of the rectangle: "))
                    width = float(input("Enter the width of the rectangle: "))
                    area = length * width
                    print("The area of the rectangle is:",area,"square units.")
                def calculate_circle_area():
                    radius = float(input("Enter the radius of the circle: "))
                    area = math.pi * radius * radius
                    print("The area of the circle is:",area," square units.")
                def check_prime_number():
                    number = int(input("Enter a number to check if it is prime: "))
                    if number > 1:
                        for i in range(2, int(math.sqrt(number)) + 1):
                            if number % i == 0:
                                print(number,"is not a prime number.")
                                break
                        else:
                            print(number, "is a prime number.")
                    else:
                        print(number, "is not a prime number.")
                def display_menu():
                    print("\nMenu:")
                    print("1. Calculate Area of Rectangle")
                    print("2. Calculate Area of Circle")
                    print("3. Check if a Number is Prime")
                    print("4. Exit")
                def main():
                    while True:
                        display_menu()
                        choice = int(input("Enter your choice (1/2/3/4): "))
                        if choice == 1:
                            calculate_rectangle_area()
                        elif choice == 2:
                            calculate_circle_area()
                        elif choice == 3:
                            check_prime_number()
                        elif choice == 4:
                            print("Exiting the program...")
                            break
                        else:
                            print("Invalid choice, please try again.")
                main()




                  624  Touchpad Computer Science (Ver. 3.0)-XI
   621   622   623   624   625   626   627   628   629   630   631