Page 521 - Computer Science V2.0 Class 11
P. 521

number = int(input("Enter a number: "))
                     reverse = reverse(number)
                     print("Reverse of the number:", reverse)

                 Program 30: Write a menu-driven program to perform the following operations:

                 1.  Square Root of number
                 2.  GCD of two numbers
                 3.  Factorial of a number

                 4.  Exit
                 Develop separate user-defined functions for each menu option.

                 Ans. import math
                     def calculateSquareRoot():
                         '''
                         Objective : To calculate the square root of a number
                         Input Parameter : None

                         Return Value : None
                         '''
                         num = float(input("Enter a number: "))
                         if num >= 0:

                             squareRoot = math.sqrt(num)
                             print(f"Square root of {num} is {squareRoot:.2f}")
                         else:
                             print("Cannot calculate square root of a negative number.")


                     def calculateGCD():

                         '''
                         Objective : To calculate the GCD of two numbers
                         Input Parameter : None
                         Return Value : None

                         '''
                         num1 = int(input("Enter the first number: "))
                         num2 = int(input("Enter the second number: "))
                         gcd = math.gcd(num1, num2)
                         print(f"GCD of {num1} and {num2} is {gcd}")


                     def calculateFactorial():
                         '''
                         Objective : To calculate the factorial of a number
                         Input Parameter : None

                         Return Value : None


                                                                                                           Practical  507
   516   517   518   519   520   521   522   523   524   525   526