Assignment Using Function
1. Design of function to print your name when it call?
def name():
print("Sadasib")
name()
2. Design a function with parameter and print the parameter?
def Clg(name):
print(name)
Clg("TRIDENT")
3. Design of Square function then input a number and calculate square using
function?
def squre(x):
return x**2
x=int(input("Enter a number: "))
out= squre(x)
print("The squre of", x,"is" , out)
4. Design of function that accepts of binary no. and converts into decimal?
def BinaryToDecimal(n):
sum=0
i=0
while(n>0):
dig=n%10
sum=sum+dig*(2**i)
i=i+1
n=n//10
return sum
n=int(input("Enter a binary number:-"))
deci=BinaryToDecimal(n)
print("Dcimal Equivalent of {} is {} " .format(n,deci))
5. Design function that can accept any number of integers and return their sum?
def Add(*a):
sum=0
for i in a:
sum=sum+i
return sum
num1=int(input("Enter a number:-"))
num2=int(input("Enter a number:-"))
res=Add(num1,num2)
print("The sum is ",res)
6. Write a single python function that can be able to calculate the area and
perimeter of a rectangle and a square?
def AreaPeri(len,bre=1):
if(bre==1):
print("Area of the squre is",(len*len))
print("perimeter of the Squre is=",(4*len))
else:
print("Area of the Rectangle",(len*bre))
print("Peremeter of the Rectangle is",(2*(len+bre)))
len=float(input("Enter the length"))
bre=float(input("Enter the breath"))
AreaPeri(len,bre==1)
7. Write a program to print using recursion?
def fibo(n):
if(n<=1):
return n
else:
return (fibo(n-1)+fibo(n-2))
n1=int(input("Enter term number:-"))
if(n1<=0):
print("Enter a positve number:-")
else:
print("Fibonaccisequence:-")
for i in range(n1):
print(fibo(i))
8. Demonstrate the use of user defined module in python?
def add (a,b):
return a+b
def sub (a,b):
return a-b
def mul (a,b):
return a*b
def div (a,b):
return a//b #Save the program in [Link]
import Calculator as cal
n1=int(input("Enter 1st number:-"))
n2=int(input("Enter 2nd number:-"))
res=[Link](n1,n2)
print("sum=",res)
res=[Link](n1,n2)
print("sub=",res)
res=[Link](n1,n2)
print("mul=",res)
res=[Link](n1,n2)
print("div=",res)
9. Design of function largest that can accept 3 numbers and return the largest
among them?
def Largest(a,b,c):
if (a>b)and (a>c):
print("Largest number is=",a)
elif ((b>a)and(b>c)):
print("Largest number is =",b)
else:
print("Largest number is=",c)
Largest(12,29,200)