0% found this document useful (0 votes)
54 views7 pages

Prac 12th 34

The document contains Python functions that perform various mathematical operations on numbers. It includes a main loop that allows the user to choose which operation to perform by entering a number 1-5. The operations include calculating number of digits, sum and product; properties of even/odd digits; Fibonacci numbers; perfect, prime, palindrome and Armstrong numbers; and factorian, harshad, happy numbers. It takes a number as input, calls the corresponding function and prints 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 ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

Prac 12th 34

The document contains Python functions that perform various mathematical operations on numbers. It includes a main loop that allows the user to choose which operation to perform by entering a number 1-5. The operations include calculating number of digits, sum and product; properties of even/odd digits; Fibonacci numbers; perfect, prime, palindrome and Armstrong numbers; and factorian, harshad, happy numbers. It takes a number as input, calls the corresponding function and prints 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 ODT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#22-ParthTakle-Q2

def sumpro():
n=int(input('input n?'))
c, s, p=0, 0, 1
while n>0:
d=n%10
c+=1
s+=d
p*=d
n//=10
print('Sum=',s)
print('Product=',p)
print('Number=',c)
def sumproavgoe():
n=int(input('input n?'))
c1=c2=p1=p2=1
s1=s2=0
while n:
d=n%10
if d%2==1:
c1+=1
s1+=d
else:
c2+=1
s2+=d
n//=10
print('Number of odd digits=',c1)
print('Number of even digits=',c2)
print('Sum of odd digits=',s1)
print('Sum of even digits=',s2)
print('Avg of odd digits=',s1/c1)
print('Avg of even digits=',s1/c1)
def prooe():
n=int(input('input n?'))
c1=c2=p1=p2=1
while n:
d=n%10
if d%2==1:
c1+=1
p1*=d
else:
c2+=1
p2*=d
n//=10
print('No. of odd digits=',c1)
print('No. of even digits=',c2)
print('Product of odd digits=',p1)
print('Product of even digits=',p2)
def fibonacci():
n=int(input('input n?'))
f1, f2=0, 1
while n>f1:
f1, f2=f2, f1+f2
if n==f1:
print('Fibonacci no.')
else: print('Not Fibonacci no.')
def hcflcm():
a=int(input('Input a?'))
b=int(input('Input b?'))
p=a*b
while a%b!=0:
r=a%b
a, b=b, r
print('HCF=',b)
print('LCM=',p//b)
while True:
print('1. Number of digit,sum of digits and product of digits')
print('2. Number of even and odd, sum of even and odd, average
of even and odd')
print('3. Number of even and odd, product of even and odd')
print('4. Fibonacci number')
print('5. HCF and LCM')
print('0. Exit')
ch=input('Choice[0-5]?')
if ch=='0': break
elif ch=='1': sumpro()
elif ch=='2': sumproavgoe()
elif ch=='3': prooe()
elif ch=='4': fibonacci()
elif ch=='5': hcflcm()
else: print('Invalid input')

'''
1. Number of digit,sum of digits and product of digits
2. Number of even and odd, sum of even and odd, average of even and
odd
3. Number of even and odd, product of even and odd
4. Fibonacci number
5. HCF and LCM
0. Exit
Choice[0-5]?1
input n?2
Sum= 2
Product= 2
Number= 1
1. Number of digit,sum of digits and product of digits
2. Number of even and odd, sum of even and odd, average of even and
odd
3. Number of even and odd, product of even and odd
4. Fibonacci number
5. HCF and LCM
0. Exit
Choice[0-5]?2
input n?3
Number of odd digits= 2
Number of even digits= 1
Sum of odd digits= 3
Sum of even digits= 0
Avg of odd digits= 1.5
Avg of even digits= 1.5
1. Number of digit,sum of digits and product of digits
2. Number of even and odd, sum of even and odd, average of even and
odd
3. Number of even and odd, product of even and odd
4. Fibonacci number
5. HCF and LCM
0. Exit
Choice[0-5]?3
input n?4
No. of odd digits= 1
No. of even digits= 2
Product of odd digits= 1
Product of even digits= 4
1. Number of digit,sum of digits and product of digits
2. Number of even and odd, sum of even and odd, average of even and
odd
3. Number of even and odd, product of even and odd
4. Fibonacci number
5. HCF and LCM
0. Exit
Choice[0-5]?4
input n?5
Fibonacci no.
1. Number of digit,sum of digits and product of digits
2. Number of even and odd, sum of even and odd, average of even and
odd
3. Number of even and odd, product of even and odd
4. Fibonacci number
5. HCF and LCM
0. Exit
Choice[0-5]?5
Input a?2
Input b?4
HCF= 2
LCM= 4
1. Number of digit,sum of digits and product of digits
2. Number of even and odd, sum of even and odd, average of even and
odd
3. Number of even and odd, product of even and odd
4. Fibonacci number
5. HCF and LCM
0. Exit
Choice[0-5]?0
'''
#22-ParthTakle-Q3
def perfect():
n=int(input('input n?'))
k, s=1, 0
while k<n:
if n%k==0: s+=k
k+=1
if n==s:
print('perfect number')
else: print('Not perfect number')
def prime():
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 palimdrome():
n=int(input('input n?'))
t ,s=n ,0
while t:
d=t%10
s=10*s+t%10
t//=10
if s==n:
print('Palindrome')
else: print('Not a palindrome')
def armstrong():
n=int(input('input n?'))
t, s, power=n, 0, len(str(n))
while t>0:
d=t%10
s+=d**power
t//=10
if n==s: 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': perfect()
elif ch=='2': prime()
elif ch=='3': palimdrome()
elif ch=='4': armstrong()
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?3
prime number
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?3
input n?434
Palindrome
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?4
input n?7
Armstrong no.
1. Perfect number
2. Prime number
3. Palindrome number
4. Armstrong number
0. exit
Choice[0-4]?0
'''
#22-ParthTakle-Q4
def factorian():
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 harshad():
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 happy():
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 fibon():
n=int(input('Input n? '))
k,f1, f2, s=0,0, 1, 0
while k<=n:
print(f1)
s+=f1
f1, f2=f2, f1+f2
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': factorian()
elif ch=='2': harshad()
elif ch=='3': happy()
elif ch=='4': fibon()
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?24
Not 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?32
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?23
23 happy number
1. Factorian number
2. Harshad number
3. Happy number
4. generate first n fibonacci number
0. exit
Choice[0-4]?4
Input n? 6
0
1
1
2
3
5
8
Sum= 20
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