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

Program 7: Write a function palindrome(num) that takes num as an input and checks whether the num is a
               palindrome or not. A palindromic number (also known as a numeral palindrome or a numeric palindrome) is a number
               (such as 16461) that remains the same when its digits are reversed. Invoke the function to print an appropriate
               message for the user-entered number.

              Ans. def palindrome(num):
                       '''

                       Objective : To check whether a number is a palindrome
                       Input Parameter : num - numeric value
                       Return Value : 1 - if number is palindrome, 0 otherwise
                       '''
                       n = num   #storing the number for later usage

                       reverseNum = 0
                       remainder = 0
                       while num > 0:
                           remainder = num %10
                           reverseNum = reverseNum * 10 + remainder
                           num //= 10
                       if n == reverseNum:

                           return 1
                       else:
                           return 0

                   num = int(input("Enter a number: "))

                   result = palindrome(num)
                   if result == 1:
                       print(num, 'is palindrome')
                   else:
                       print(num, 'is not palindrome')
               Program 8: Write a function checkPrime(num) that takes num as an input and checks whether num is a prime or
               not. Prime numbers are natural numbers that are divisible by only 1 and the number itself. Invoke the function to print
               an appropriate message for the user-entered number.

              Ans. def checkPrime(num):
                       '''

                       Objective: To check whether a number is prime.
                       Input: num: the number to be tested for primeness
                       Return Value: The message indicating whether n is prime
                       '''


                       upperLimit = num
                       for i in range(2, num):
                           if num % i == 0:

               492   Touchpad Computer Science (Ver. 2.0)-XI
   501   502   503   504   505   506   507   508   509   510   511