Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to calculate the volume and area of the Cylinder
In this article, we will look into a python program to calculate the volume and area of a cylinder.
A cylinder is defined as a 3D object that has two circles connected with a rectangular surface. The special thing about a cylinder is that even though it is measured using just two dimensions, i.e. the height and radius, the cylinder is considered a three-dimensional figure as it is measured in xyz coordinate axes.
The area of the cylinder is calculated in two ways ? area of the lateral surface and area of the total surface. Lateral surface area is only the area of the rectangular surface connecting the base circles while the total surface area is the area of the entire cylinder.
Volume of a cylinder is defined as the space contained by the said object.
Mathematical Formulae
The mathematical formulae to calculate surface areas of the cylinder are as follows ?
Lateral Surface Area: 2?rh Total Surface Area: 2?r(r + h)
The volume of a cylindrical object is calculated using the following formula ?
Volume: ?r²h
Input Output Scenario
The python program to calculate the surface areas and volume of the cylindrical object will provide the following input-output scenario ?
Assume the height and radius of the cylinder as shown below, the output is obtained as ?
Input: (6, 5) // 6 is the height and 5 is the radius Result: Lateral Surface Area of the cylinder: 188.50 Total Surface Area of the cylinder: 345.58 Volume of the cylinder: 471.24
Using Mathematical Formulae
The standard mathematical formulae are applied to calculate the area and volume of the cylinder. We require the values of height and radius of the cylinder to substitute them in the formulae and obtain the surface areas and volume of the 3D object.
Example
In the example code below, we import the math library from python to use the pi constant while solving for the area and volume. The inputs are considered as the height and radius of the cylindrical figure ?
import math
# height and radius of the cylinder
height = 6
radius = 5
# calculating the lateral surface area
cyl_lsa = 2 * (math.pi) * (radius) * (height)
# calculating the total surface area
cyl_tsa = 2 * (math.pi) * (radius) * (radius + height)
# calculating the volume
cyl_volume = (math.pi) * (radius) * (radius) * (height)
# displaying the area and volume
print("Lateral Surface Area of the cylinder:", str(cyl_lsa))
print("Total Surface Area of the cylinder:", str(cyl_tsa))
print("Volume of the cylinder:", str(cyl_volume))
The output of the above code is ?
Lateral Surface Area of the cylinder: 188.49555921538757 Total Surface Area of the cylinder: 345.57519189487726 Volume of the cylinder: 471.23889803846896
Using Functions
We can also make use of user-defined functions in python to compute the area and volume. These functions in python are declared using the def keyword, with arguments passed being height and radius of the cylinder.
Example
The following python program makes use of functions to compute the surface areas and volume of cylinder ?
import math
def cyl_surfaceareas(height, radius):
# calculating the lateral surface area
cyl_lsa = 2 * (math.pi) * (radius) * (height)
print("Lateral Surface Area of the cylinder:", str(cyl_lsa))
# calculating the total surface area
cyl_tsa = 2 * (math.pi) * (radius) * (radius + height)
print("Total Surface Area of the cylinder:", str(cyl_tsa))
def cyl_volume(height, radius):
# calculating the volume
cyl_volume = (math.pi) * (radius) * (radius) * (height)
# displaying the volume
print("Volume of the cylinder:", str(cyl_volume))
# height and radius of the cylinder
height = 7
radius = 4
cyl_surfaceareas(height, radius)
cyl_volume(height, radius)
The output of the above code is ?
Lateral Surface Area of the cylinder: 175.92918860102841 Total Surface Area of the cylinder: 276.46015351590177 Volume of the cylinder: 351.85837720205683
Conclusion
Python provides simple ways to calculate cylinder properties using mathematical formulae with the math.pi constant. Both direct calculation and function-based approaches work effectively for computing lateral surface area, total surface area, and volume of cylinders.
