WORK SHEET 8 Solutions Functions
WORK SHEET 8 Solutions Functions
Solution
1.Square root of a number Given a number A. Return square root of the number if it is perfect square
otherwise return-1. Note: A number is a perfect square if its square root is an integer.
Ans:
def sq_root(a):
b=pow(a,.5)
if(pow(b,2)==a):
print(b)
else:
print("-1")
a=int(input())
sq_root(a)
2.Area of Square You are given a positive integer A denoting the side of a square. You have to calculate
the area of the square. Area of a square having side S is given by (S * S).
Ans:
def area(a):
area=a*a
print(area)
a=int(input("Enter the side of square:"))
area(a)
3.Area of Circle You are given a positive integer A denoting the radius of a circle. You have to calculate
the area of the circle.
Ans:
def area(r):
area=3.14*r*r
print(area)
a=float(input())
area(a)
4.Power function You are given two integers A and B.You have to find the value of A^B.
Ans:
def power(A,B):
res=pow(A,B)
print(res)
A=int(input("Enter the value of A:"))
B=int(input("Enter the value of B:"))
power(A,B)
5.Cube It! You are given an integer A.You have to find the value of cube of A i.e, A^3.
Ans:
def cube(A):
res=pow(A,3)
return res
a=int(input("Enter the value of a:"))
print(cube(a))
6.Volume Of Sphere You are given a positive integer A denoting the radius of a sphere. You have to
calculate the volume of the sphere. Volume of a sphere having radius R is given by (4 * π * R3) / 3.
Ans:
def vol_of_sphere(A):
res=(4*3.14*A*A*A)/3
return res
a=float(input("Enter the radius of sphere:"))
print(vol_of_sphere(a))
7.Area Of Ellipse Given the lengths of semi-major axis A and semi-minor axis B of an ellipse, calculate the
Area of the Ellipse. Area of ellipse having semi-major axis length a and semi-minor axis length b is given
by π * a * b.
Ans:
def ar_of_ell(a,b):
res=3.14*a*b
return res
a=float(input("Enter the semi major axis:"))
b=float(input("Enter the semi minor axis:"))
print(ar_of_ell(a,b))
8.Sum the Array Write a program to print sum of elements of the input array A of size N.
Ans:
def sum_of_arr(c):
return(sum(c))
Ans:
def sq(x,y):
for i in range(x,y+1):
print(pow(i,2))
x=int(input("Enter the staring point:"))
y=int(input("Enter the end point:"))
sq(x,y)
Ans:
def s(a,b):
return a+b
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
print(s(a,b))
Ans:
def calc(a,b,op):
if op==1:
return a+b
elif op==2:
return a-b
elif op==3:
return a*b
elif op==4:
return a/b
elif op==5:
return a%b
a=int(input())
b=int(input())
op=int(input())
print(calc(a,b,op))
Ans:
def pythagores(a,b,c):
if(pow(a,2)+pow(b,2)==pow(c,2)):
print("Verified")
else:
print("Not verified")
a=int(input())
b=int(input())
c=int(input())
pythagores(a,b,c)