Page 436 - Computer Science Class 11 With Functions
P. 436

Program 21

        Write a program to display the area and perimeter of a rectangle. Define two separate functions, returning area and
        perimeter of a rectangle, respectively, for the user-inputted length and breadth.
        Ans. def rectangleArea(length, width):

                 '''
                 Objective : To calculate the area of a rectangle
                 Input Parameter : length , width – numeric value
                 Return Value : length * width – numeric value
                 '''
                 return length * width



             def rectanglePerimeter(length, width):
                 '''
                 Objective : To calculate the perimeter of a rectangle

                 Input Parameter : length , width – numeric value
                 Return Value : 2*(length + width) – numeric value
                 '''
                 return 2 * (length + width)


             length = float(input("Enter the length of the rectangle: "))

             width = float(input("Enter the width of the rectangle: "))
             area = rectangleArea(length, width)
             perimeter = rectanglePerimeter(length, width)
             print(f"Area of the rectangle: {area}")

             print(f"Perimeter of the rectangle: {perimeter}")
        Program 22
        Define two separate functions, returning volume and surface area of a sphere, respectively, for the user-inputted
        radius.

        Ans. import math
             def sphereVolume(radius):

                 '''
                 Objective : To calculate the volume of a sphere
                 Input Parameter : radius – numeric value
                 Return Value : volume – numeric value
                 '''

                 volume = (4/3) * math.pi * radius**3
                 return volume



             def sphereSurfaceArea(radius):


         434   Touchpad Computer Science-XI
   431   432   433   434   435   436   437   438   439   440   441