हिंदी

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

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 2: Concept of Object Oriented Programming - EXCERCISE [पृष्ठ ३८]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 2 Concept of Object Oriented Programming
EXCERCISE | Q 17. | पृष्ठ ३८
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×