CODING JOY

The stunning potpourri of coding and mundane life

SPY NUMBER IN PYTHON LANGUAGE



AMRUHA AHMED
12th May,2023.



A number whose sum of digits is equal to the product of digits is said to be a Spy Number.

blog1

DELVE DEEPER:

In order to check whether the given number is a spy number or not, we require two functions :


ALGORITHM FOR SUM_OF_DIGITS FUNCTION:


ALGORITHM FOR PRODUCT_OF_DIGITS FUNCTION:

The following Python program checks whether the given number is a Spy Number or not:


PROGRAM:

                
                  def sum_of_digits(n):
                    s=0 #sum
                    while n!=0 :
                      rem=n%10 #extracting the last digit of n
                      s=s+rem #calculating sum
                      n=n//10 #updating n
                    return s
                  def product_of_digits(n):
                    p=1 #product
                    while n!=0:
                      rem=n%10 #extracting the last digit of n
                      p=p*rem #calculating product
                      n=n//10 #updating n
                    return p
                  n=int(input("Enter a number:")) #n-number 
                  sum=sum_of_digits(n) #storing value of sum_of_digits() in sum
                  product=product_of_digits(n) #storing value of product_of_digits() in product
                  if(sum==product): #checking condition for spy number
                    print("{} is a Spy Number".format(n))
                  else:
                    print("{} is not a Spy Number".format(n))

             
         
CODE COPIED

DRY RUN:


Suppose n=123 is passed as parameter to the sum function . The values of s,rem and n will be:
blog1
If we pass n=123 as parameter to the product function, the values of n,rem and p will be:
blog1
Since value returned from sum function is 6 and the value returned from product function is also 6, hence the number 123 is a spy number.


OUTPUT:

Enter a number:123
123 is a Spy Number


OUTPUT:

Enter a number:112
112 is not a Spy Number