# Python Program to Check a Number is Spy number

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Given a number, the task is to check whether the given number is a spy number or not using Python.

Spy number:

If the sum of a number’s digits equals the product of its digits, it’s called a Spy number. 1124,123,1412,132……

Examples:

Example1:

Input:

Given number=1124

Output:

The given number 1124 is spy number

Example2:

Input:

Given number=123

Output:

The given number 123 is spy number

## Program to Check a Number is Spy number in Python

Below are the ways to check the given number is a spy number or not in Python.

### Method #1: Using For Loop (Static Input)

Approach:

• Give the number as static input and store it in a variable.
• Convert this given number to a string using the function str().
• Take two variables one to store the sum of digits and another to store the product of digits and initialize the sum of digits to 0 and the product of digits with 1.
• Convert this given number into a list of digits using list(),map(),int functions.
• Traverse through the digits of the number(Iterate through the list) using For loop.
• For each element add the element to the sum of digits.
• Multiply the element with the product of digits.
• Check if the sum of digits is equal to the product of digits using the If statement.
• If it is true then it is a spy number
• Else it is not a spy number.
• The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numbe = 1124
# Convert this given number to a string using the function str().
strnumbe = str(numbe)
# Take two variables one to store the sum of digits and
# another to store the product of digits and
# initialize the sum of digits to 0 and the product of digits with 1.
sumOfDigit = 0
productOfDigit = 1
# Convert this given number into a list of digits using list(),map(),int functions.
listdigits = list(map(int, strnumbe))
# Traverse through the digits of the number(Iterate through the list) using For loop.
for elemen in listdigits:
# For each element add the element to the sum of digits.
# Multiply the element with the product of digits.
sumOfDigit = sumOfDigit+elemen
productOfDigit = productOfDigit*elemen
# Check if the sum of digits is equal to the product of digits using the If statement.
# If it is true then it is a spy number
if(sumOfDigit == productOfDigit):
print('The given number', strnumbe, 'is spy number')
# Else it is not a spy number.
else:
print('The given number', strnumbe, 'is not spy number')


Output:

The given number 1124 is spy number

### Method #2: Using For Loop (User Input)

Approach:

• Give the number as user input using int(input()) and store it in a variable.
• Convert this given number to a string using the function str().
• Take two variables one to store the sum of digits and another to store the product of digits and initialize the sum of digits to 0 and the product of digits with 1.
• Convert this given number into a list of digits using list(),map(),int functions.
• Traverse through the digits of the number(Iterate through the list) using For loop.
• For each element add the element to the sum of digits.
• Multiply the element with the product of digits.
• Check if the sum of digits is equal to the product of digits using the If statement.
• If it is true then it is a spy number
• Else it is not a spy number.
• The Exit of the Program.

Below is the implementation:

# Give the number as user input using int(input()) and store it in a variable.
numbe = int(input('Enter some random number = '))
# Convert this given number to a string using the function str().
strnumbe = str(numbe)
# Take two variables one to store the sum of digits and
# another to store the product of digits and
# initialize the sum of digits to 0 and the product of digits with 1.
sumOfDigit = 0
productOfDigit = 1
# Convert this given number into a list of digits using list(),map(),int functions.
listdigits = list(map(int, strnumbe))
# Traverse through the digits of the number(Iterate through the list) using For loop.
for elemen in listdigits:
# For each element add the element to the sum of digits.
# Multiply the element with the product of digits.
sumOfDigit = sumOfDigit+elemen
productOfDigit = productOfDigit*elemen
# Check if the sum of digits is equal to the product of digits using the If statement.
# If it is true then it is a spy number
if(sumOfDigit == productOfDigit):
print('The given number', strnumbe, 'is spy number')
# Else it is not a spy number.
else:
print('The given number', strnumbe, 'is not spy number')


Output:

Enter some random number = 123
The given number 123 is spy number

Related Programs: