Page 294 - Computer Science Class 11 With Functions
P. 294

finish = 10000
                  for n in range(start, finish, 11):
                      #if unit digit equals thousand's digit and
                      #   tens digit equals hundreds digit,
                      #then n is a palindrome
                      if ((n//1000)%10 == n%10) and ((n//100)%10 == (n//10)%10):
                          count+=1
                  return count
              #Print the number of 4-digit palindromes divisible by 11
              print('No of 4-digit palindromes divisible by 11 is ', div11Palindromes())
           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  ")

         292   Touchpad Computer Science-XI
   289   290   291   292   293   294   295   296   297   298   299