Python Program to calculate the volume and area of the Cylinder

PythonServer Side ProgrammingProgramming

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 is 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 of 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: πr2h

Input Output Scenarios

The python program to calculate the surface areas and volume of the cylindrical object will provide the following input-output scenarios −

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.49555921538757
Total Surface Area of the cylinder: 345.57519189487726
Volume of the cylinder: 471.2388980384689

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))

Output

After executing the code above, the output is displayed as −

Lateral Surface Area of the cylinder: 188.49555921538757
Total Surface Area of the cylinder: 345.57519189487726
Volume of the cylinder: 471.23889803846896

Function to Calculate Area and Volume

We can also make use of the 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. Let us look at the example below.

Example

The following python program makes use of function 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 area and 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)

Output

After executing the python code, the output displays the surface area and volume −

Lateral Surface Area of the cylinder: 175.92918860102841
Total Surface Area of the cylinder: 276.46015351590177
Volume of the cylinder: 351.85837720205683
raja
Updated on 14-Oct-2022 08:05:51

Advertisements