DEV Community

Cover image for Python tasks - 7
G Gokul
G Gokul

Posted on

Python tasks - 7

Task - Neon number

A neon number is a positive integer whose sum of the digits of its square, equals the original number. For example, 9 is a neon number because 9^2=81 and 8+1=9.
Program:

def sum_of_digits(no):
    sum = 0
    while no > 0:
        sum = sum + no%10
        no = no//10
    return sum
number = 9 
square = number ** 2
sum = sum_of_digits(square)
if number == sum: 
    print('Neon Number')

Enter fullscreen mode Exit fullscreen mode

Output:
Neon Number

Task - Strong number

Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Given a number, the task is to check if it is a Strong Number or not.
Program:

def find_factorial(no):
        factorial = 1
        while no > 0:
            factorial *= no 
            no-=1 
        return factorial 

def digits(no): 
    total = 0
    while no > 0:
        digit = no % 10
        factorial = find_factorial(digit) 
        total += factorial
        no = no//10
    return total 
number = int(input("enter a number:"))                       
result = digits(number) 
if result == number: 
    print("Strong Number")
Enter fullscreen mode Exit fullscreen mode

Output:
enter a number:145
Strong Number

Task - Automorphic Number

An automorphic number is a number whose square ends with the same digits as the number itself, such as 25 (because 25² = 625) or 76 (because 76² = 5776).
Program:

no = int(input("Enter a number: "))
square = no ** 2
digit = 0
no_digit = no
while no_digit> 0:
    digit += 1
    no_digit //= 10
last_digits = square % (10 ** digit)
if no == last_digits:
    print("Automorphic Number")
else:
    print("Not an Automorphic Number")
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 5
Automorphic Number

Task - Duck Number:

A Duck Number is a positive integer that contains at least one 0, but it must not start with 0. For example, 2046 and 2100 are duck numbers, while 0123 is not.
Program:

no = input("Enter no:")
if no[0] == '0':
    print('Not a Duck Number')
else:
    if '0' in no:
        print("Duck Number")
    else:
        print('Not a Duck Number')
Enter fullscreen mode Exit fullscreen mode

Output:
Enter no:1223
Not a Duck Number

Task - Spy Number:

Your are given a number n . The number is said to be a Spy number if the sum of all the digits is equal to the product of all digits.
Eg. 1412
Program:

def sum_of_digits(no):
    sum = 0
    multiply = 1 
    while no > 0:
        sum = sum + no%10
        multiply = multiply * no%10
        no = no//10
    return sum == multiply 

no = 123
result = sum_of_digits(no)
print('Spy Number: ', result)
Enter fullscreen mode Exit fullscreen mode

Output:
Spy Number: True

Top comments (0)