Dremendo Tag Line

Input a number and check if it is a special two-digit number or not using a function in Python

Function - Question 6

In this question, we will see how to input a number and check if it is a special two-digit number or not in Python programming using a function. To know more about function click on the function lesson.

Q6) Write a program in Python to input a number and check if it is a special two-digit number or not using a function. The function should return 1 if the numbers is a special two-digit number else return 0.

A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.

Example: 59 is a special two-digit number.
Sum of its digits 5+9 = 14
Product of its digit 5*9 = 45
Sum of sum and product of the digits 14+45 = 59

Program

def specialnumber(num):
    s=0
    p=1
    t=num

    if t>=10 and t<=99:
        while t>0:
            d=t%10
            s=s+d   # sum of digits
            p=p*d   # product of digits
            t=t//10

        if s+p==num:
            return 1

    return 0


# main program
n=int(input('Enter a number '))
if specialnumber(n)==1:
    print('Special Two Digit Number')
else:
    print('Not Special Two Digit Number')

Output

Enter a number 59
Special Two Digit Number
video-poster