Advertisements
Advertisements
प्रश्न
Write a program to find out volume of the following using function overloading.
- volume of cube
- volume of cuboid
- volume of cylinder
कोड लेखन
Advertisements
उत्तर
Python simulates function overloading here with optional parameters: one argument is for a cube, two arguments are for a cylinder, and three arguments are for a cuboid.
def volume(a, b=None, c=None):
if b is None and c is None:
# Volume of cube
return a * a * a
elif c is None:
# Volume of cylinder
return 3.14 * a * a * b
else:
# Volume of cuboid
return a * b * c
# Cube
s = float(input("Enter side of cube: "))
print("Volume of cube =", volume(s))
# Cuboid
l = float(input("Enter length of cuboid: "))
b = float(input("Enter breadth of cuboid: "))
h = float(input("Enter height of cuboid: "))
print("Volume of cuboid =", volume(l, b, h))
# Cylinder
r = float(input("Enter radius of cylinder: "))
h = float(input("Enter height of cylinder: "))
print("Volume of cylinder =", volume(r, h))
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
