Write a Python Program Using The Function to Print Area of Rectangle And Area of Circle

Given the Length and width of a rectangle as an input to the Python program,  and also that given that the radius of a circle is an input to the Python program,  your task is to calculate the area of a rectangle and area of a circle and print it.




Area of a rectangle is defined as

area = length x width




Area of a circle is defined as

area =  pi * radius ^ 2 = pi * radius * radius

Lets ask user to enter parameters of rectangle and circle and then find the area of a  rectangle and circle

 

 

Write a Python Program Using The Function to Print Area of Rectangle And Area of Circle

 


Write a Python Program Using The Function to Print Area of Rectangle And Area of Circle

Import the required libraries,  we need a math library in order to get the  pi value from the math library.

import math





Define a function named as a rectangle by passing the input values to the function as Length and width and using printf function call to print the area of a rectangle by multiplying the Length and width of a rectangle.

def rectangle(l, w):
   print("Area of a Rectangle is : %.2f" %(l * w))





Define another function named as circle by passing the radius as a input to the Python function and using the print function called print the area of a circle by multiply  (pi value * radius * radius).  the product of this will give us the area of a circle.

Using the print statement print the area of a circle

def circle(r):
   print("Area of Circle is : %.2f" %(math.pi * r * r))





In the main program take a variable Length and width and ask a user to enter the Length and width of a rectangle using the input function call then convert these values to the floating point values.

Once we get the Length and width of a rectangle, call the function that is a rectangle by passing the input values that are Length and width.

length = float(input("Enter Rectangle Length : "))
width = float(input("Enter Rectangle Width : "))
rectangle(length, width)





After calculating the area of a rectangle, ask a user to enter the radius of a circle using the input function call by taking a variable radius,  converting this value to the floating point  value.  then call the function Circle to get the area of a circle by passing the input value as radius of a circle.

radius = float(input("Enter Radius of Circle : "))
circle(radius)






Complete program
=====================

import math


def rectangle(l, w):
   print("Area of a Rectangle is : %.2f" %(l * w))


def circle(r):
   print("Area of Circle is : %.2f" %(math.pi * r * r))


length = float(input("Enter Rectangle Length : "))
width = float(input("Enter Rectangle Width : "))
rectangle(length, width)


radius = float(input("Enter Radius of Circle : "))
circle(radius)





Output
================

Enter Rectangle Length : 12.6
Enter Rectangle Width : 78.03
Area of a Rectangle is : 983.18
Enter Radius of Circle : 34
Area of Circle is : 3631.68




Output 3
==================

Enter Rectangle Length : 44.4
Enter Rectangle Width : 23.33
Area of a Rectangle is : 1035.85
Enter Radius of Circle : 900
Area of Circle is : 2544690.05



Conclusion
==================

Execute the above program by providing the random input values to the Python program and note down the result coming down if you have any queries or suggestions.

Post a Comment

0 Comments