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

4.  Define a function divisibleDivisors(n, k) which takes n and k (n>=k)as its arguments and returns  the number
                    of divisors of n which are divisible by k. Make use of the function to accept the values of n and k from the user and print
                    the number of divisors of n which are divisible by k.
               Ans.  def divisibleDivisors(n, k):
                        '''
                        Objective: Given n>=k, find the count of divisors of n which are divisible by k
                        Input Parameter: n,k - Integers
                        Return Value: count of divisors of n which are divisible by k
                        '''
                        assert n>=k
                        count = 0
                        for i in range(1, n + 1):
                            if (n % i == 0 and i % k == 0):
                                count += 1
                        return count




                    n = int(input('Enter a number n:'))
                    k = int(input('Enter k, n>=k:'))
                    count = divisibleDivisors(n, k)
                    print('number of divisors of', n, 'divisible by', k, '= ', count)
                 5.  Define four functions—addition, subtraction, multiplication, and division that takes two numbers as input from the user
                    and returns the result of computation. Write a menu-driven program that takes two numbers as input from the user and
                    invokes appropriate function for addition, subtraction, multiplication, and division depending upon user's choice.
               Ans.  #Objective: To form a calculator
                    def menu():
                        """
                        Objective: To display the menu
                        Inputs: NIL
                        Return value:
                            ch : Serial number of menu item selected by the user
                        """
                        print("    MENU OPTIONS FOR CALCULATOR     ")
                        print("  1. Add  ")
                        print("  2. Multiply  ")
                        print("  3. Subtract  ")
                        print("  4. Divide  ")
                        ch=int(input("Enter your choice :   "))
                        return ch
                    def addition(num1, num2):
                        '''
                        Objective: To calculate sum
                        Inputs:
                        num1, num2 : numbers to be added
                        Return value: NIL
                        '''
                        sum = num1 + num2
                        print("The sum is ",sum)
                    def product(num1, num2):

               244   Touchpad Computer Science (Ver. 2.0)-XI
   253   254   255   256   257   258   259   260   261   262   263