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

Program 5

            Write a function perfectNumber(number) that takes an integer argument number as an input and checks
            whether the number is a perfect square or not. In number theory, a perfect number is a positive integer that is
            equal to the sum of its positive divisors, excluding the number itself. Invoke the function to print an appropriate
            message for the user-entered number.

            Ans. def perfectNumber(number):
                     '''
                     Objective : To check whether a number is perfect number

                     Input Parameter : number - numeric value
                     Return Value : 1 - if number is perfect, 0 otherwise
                     '''
                     sum = 0
                     for i in range(1, number):
                         if(number % i == 0):

                             sum = sum + i
                     if (sum == number):
                         return 1
                     else:

                         return 0
                 number = int(input("Please enter any Number: "))
                 result = perfectNumber(number)
                 if result == 1:
                     print(number, 'is a perfect number')
                 else:

                     print(number, 'is not a perfect number')
            Program 6

            Write  a  function  armstrongNumber(num)  that  takes  num  as  an  input  and  checks  whether  the  num is an
            armstrong number or not. The Armstrong number is a number that is equal to the sum of the cubes of its digits.
            For example, 0, 1, 153, 370, 371, and 407 are the Armstrong numbers. Invoke the function to print an appropriate
            message for the user-entered number.

            Ans. def armstrongNumber(num):
                     '''
                     Objective : To check whether a number is an armstrong number
                     Input Parameter : num - numeric value
                     Return Value : 1 - if number is armstrong, 0 otherwise
                     '''

                     n = num   #storing the number for later usage
                     sumCubeDigits = 0
                     while n > 0:
                         sumCubeDigits += (n % 10) * (n % 10) * (n % 10)




                                                                                                      Practical  423
   420   421   422   423   424   425   426   427   428   429   430