0% found this document useful (0 votes)
13 views5 pages

Prac

The document contains Python functions to check if a number is: 1) Perfect, prime, palindrome or Armstrong 2) Factorian, harshad, happy or generate the first n Fibonacci numbers It displays a menu for the user to select a check and inputs a number, then calls the corresponding function and displays the output.

Uploaded by

Parth Takle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Prac

The document contains Python functions to check if a number is: 1) Perfect, prime, palindrome or Armstrong 2) Factorian, harshad, happy or generate the first n Fibonacci numbers It displays a menu for the user to select a check and inputs a number, then calls the corresponding function and displays the output.

Uploaded by

Parth Takle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#23-Rigved-Q.

3
def chkperfect():
n=int(input('input n?'))
k, s=1, 0
while k<n:
if n%k==0: s+=k
k+=1
if s==n:
print('perfect number')
else: print('Not perfect number')
def chkprime():
n=int(input('input n?'))
k, prime=2, 1
while k<n:
if n%k==0:
prime=0
break
k+=1
if prime:
print('prime number')
else: print('composite number')
def chkpalimdrome():
n=int(input('input n?'))
t ,s=n ,0
while t:
s=10*s+t%10
t//=10
if s==n:
print(n,'Palindrome')
else: print(n,'Not a palindrome')
def chkarmstrong():
n=int(input('input n?'))
t, s, power=n, 0,len(str(n))
while t>0:
s+=(t%10)**power
t//=10
if s==n:
print('Armstrong no.')
else: print('Not Armstrong no.')
while True:
print('1. Perfect number')
print('2. Prime number')
print('3. Palindrome number')
print('4. Armstrong number')
print('0. exit')
ch=input('Choice[0-4]?')
if ch=='0': break
elif ch=='1': chkperfect()
elif ch=='2': chkprime()
elif ch=='3': chkpalimdrome()
elif ch=='4': chkarmstrong()
else: print('Invalid input')

'''
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?1
input n?6
perfect number
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?2
input n?23
prime number
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?3
input n?101
101 Palindrome
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?4
input n?371
Armstrong no.
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?0

'''
#23-Rigved-Q.4
def chkfact():
n=int(input('input n?'))
s, t=0, n
while t:
d=t%10
f=1
for k in range(1,d+1): f*=k
s+=f
t//=10
if s==n:
print('Factorian number')
else: print('Not Factorian number')
def chkharshad():
n=int(input('input n?'))
t, s=n, 0
while t:
s+=t%10
t//=10
if n%s==0:
print('harshad number')
else: print('harshad number')
def chkhappy():
n=int(input('input n?'))
t=n
while t!=1 and t!=4:
s=0
while t:
d=t%10
s+=d*d
t//=10
t=s
if t==1:
print(n,'happy number')
else: print(n,'unhappy number')
def genfnfibo():
n=int(input('input n?'))
f1=f2=1
s=0
k=3
print(f1)
print(f2)
while k<=n:
f3=f1+f2
print(f3)
f1=f2
f2=f3
s+=f3
k+=1
print('Sum=',s)
while True:
print('1. Factorian number')
print('2. Harshad number')
print('3. Happy number')
print('4. generate first n fibonacci number')
print('0. exit')
ch=input('Choice[0-4]?')
if ch=='0': break
elif ch=='1': chkfact()
elif ch=='2': chkharshad()
elif ch=='3': chkhappy()
elif ch=='4': genfnfibo()
else: print('Invalid input')

’’’
1. Factorian number
2. Harshad number
3. Happy number
4. generate first n fibonacci number
0. exit
Choice[0-4]?1
input n?145
Factorian number
1. Factorian number
2. Harshad number
3. Happy number
4. generate first n fibonacci number
0. exit
Choice[0-4]?2
input n?12
harshad number
1. Factorian number
2. Harshad number
3. Happy number
4. generate first n fibonacci number
0. exit
Choice[0-4]?3
input n?49
49 happy number
1. Factorian number
2. Harshad number
3. Happy number
4. generate first n fibonacci number
Choice[0-4]?4
input n?10
1
1
2
3
5
8
13
21
34
55
Sum= 141
1. Factorian number
2. Harshad number
3. Happy number
4. generate first n fibonacci number
0. exit
Choice[0-4]?0

’’’

You might also like