Practical 11 Py
Practical 11 Py
1.Write a Python function that takes a number as a parameter and check the number is
prime or not.
def prime(n):
if n>1:
for i in range(2,n):
break
if(n%i)==0:
print(n,"number is not prime")
else:
print(n,"number is prime")
a=int(input("Enter number:-"))
prime(a)
2. Write a Python function to calculate the factorial of a number (a non negative integer).
The function accepts the number as an argument.
def fact(num):
fact=1
if(num<0):
print("Factorial does not exist for negative number")
elif(num==0):
print("Factorial of 0 is 1")
else:
for i in range(1,num+1):
fact=fact*i
print("factorial of number:",fact)
n=int(input("Enter a number="))
fact(n)
3. Write a Python function that accepts a string and calculate the number of upper case
letters and lower case letters.
def fun(str):
lower=0
upper=0
for i in range(len(str)):
if(str[i]>='A' and str[i]<='Z'):
upper+=1
elif(str[i]>='a' and str[i]<='z'):
lower+=1
else:
print(" ")
return(upper,lower)
str="NMPI computer dept"
print("College name=",str)
u,l=fun(str)
print("Uppercase character=",u)
print("Lowercase character=",l)