Advertisements
Advertisements
प्रश्न
Write a program to find area of following using function overloading.
- Area of circle (function with one parameter)
- Area of rectangle (function with two parameters)
- Area of triangle (function with three parameters)
कोड लेखन
Advertisements
उत्तर
Python does not support traditional function overloading. The following program simulates it using optional parameters: one argument calculates circle area, two calculate rectangle area, and three calculate triangle area using Heron’s formula.
import math
def area(a, b=None, c=None):
if b is None and c is None:
# Area of circle
return 3.14 * a * a
elif c is None:
# Area of rectangle
return a * b
else:
# Area of triangle using Heron's formula
s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))
r = float(input("Enter radius of circle: "))
print("Area of circle =", area(r))
l = float(input("Enter length of rectangle: "))
b = float(input("Enter breadth of rectangle: "))
print("Area of rectangle =", area(l, b))
x = float(input("Enter first side of triangle: "))
y = float(input("Enter second side of triangle: "))
z = float(input("Enter third side of triangle: "))
print("Area of triangle =", area(x, y, z))
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
