Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

PROGRAM 9.

USER DEFINED MODULE – 1


AIM:
Create a module NUMBER to include the following functions, with each function to include
docstrings to describe the function. Also write a python code to import this module and use
the two functions for inputs from the user.
i) palindrome() to take as parameter a number and returns 1 if the number is a palindrome
and –1 otherwise. This function to be tested with a tuple of integers
ii) special() – takes as parameter a number and returns 1 if it’s a special number and -1
otherwise. [ A special number is a number equal to the sum of the factorial of the individual
digits of the number] This function to be tested to generate list of special numbers between
two limits accepted from the user.

PROGRAM CODING
#NUMBER.PY
#palindrome without using strings
def palindrome(num):
''' A Number is said to be Palindrome if the original
and the reverse number are same'''
b=0
temp=num
while num>0:
a=num%10
b=b*10+a
num=num//10
if temp==b: return 1
else: return -1
def splnum(num):
'''A special number is a number equal to the sum
of the factorial of the individual digits of the number'''
import math
s,b,tem=0,0,num
while num>0:
a=num%10
s+=math.factorial(a)
num=num//10
if s== tem:
return 1
else:
return -1
#******* PROGRAM9.PY
import NUMBER
print("\t\t\tTo check palindrome numbers from a tuple ")
print("\t\t\t",'*' * 45)
t=tuple()
n=int(input('Enter the no.of entries:'))
for i in range(n):
num=int(input('Enter a number:'))
t+=(num,)

for i in t:
if NUMBER.palindrome(i)==1:
print(i,'is a palindrome')
else:
print(i,'is not a palindrome')

print("\t\t\tTo check special numbers from a range ")


print("\t\t\t",'*' * 40)

lower=int(input("Enter the lower limit"))


upper=int(input("Enter the upper limit"))
lst=[]
for j in range(lower,upper+1):
if NUMBER.splnum(j)==1:
lst.append(j)
if lst==[]:
print("No special number between the given limit")
else:
print("Special number between the given limit is",lst)
OUTPUT
To check palindrome numbers from a tuple
*********************************************
Enter the no.of entries:5
Enter a number:23
Enter a number:44
Enter a number:56
Enter a number:77
Enter a number:121
23 is not a palindrome
44 is a palindrome
56 is not a palindrome
77 is a palindrome
121 is a palindrome
To check special numbers from a range
****************************************
Enter the lower limit10
Enter the upper limit1000
Special number between the given limit is [145]
>>>

You might also like