0
0
PythonProgramBeginner · 2 min read

Python Program to Find Volume of Cylinder

To find the volume of a cylinder in Python, use the formula volume = 3.1416 * radius ** 2 * height and write a program that takes radius and height as inputs and calculates this value.
📋

Examples

Inputradius = 1, height = 1
OutputVolume of cylinder: 3.1416
Inputradius = 3, height = 5
OutputVolume of cylinder: 141.372
Inputradius = 0, height = 10
OutputVolume of cylinder: 0.0
🧠

How to Think About It

To find the volume of a cylinder, first understand that the volume is the area of the circular base times the height. The base area is calculated using the formula for the area of a circle, which is pi times radius squared. Then multiply this area by the height to get the volume.
📐

Algorithm

1
Get the radius of the cylinder from the user
2
Get the height of the cylinder from the user
3
Calculate the base area using pi times radius squared
4
Multiply the base area by the height to get the volume
5
Display the volume
💻

Code

python
radius = float(input('Enter the radius of the cylinder: '))
height = float(input('Enter the height of the cylinder: '))
pi = 3.1416
volume = pi * radius ** 2 * height
print(f'Volume of cylinder: {volume}')
Output
Enter the radius of the cylinder: 3 Enter the height of the cylinder: 5 Volume of cylinder: 141.372
🔍

Dry Run

Let's trace the example where radius = 3 and height = 5 through the code

1

Input radius

User enters radius = 3

2

Input height

User enters height = 5

3

Calculate volume

volume = 3.1416 * 3 ** 2 * 5 = 3.1416 * 9 * 5 = 141.372

4

Print result

Output: Volume of cylinder: 141.372

StepOperationValue
1radius input3
2height input5
3volume calculation141.372
4print outputVolume of cylinder: 141.372
💡

Why This Works

Step 1: Calculate base area

The base area of the cylinder is a circle, so we use pi * radius ** 2 to find it.

Step 2: Multiply by height

Volume is base area times height, so multiply the area by height to get the volume.

Step 3: Display the volume

Print the calculated volume so the user can see the result.

🔄

Alternative Approaches

Using math.pi constant
python
import math
radius = float(input('Enter radius: '))
height = float(input('Enter height: '))
volume = math.pi * radius ** 2 * height
print(f'Volume of cylinder: {volume}')
This uses the built-in math module for a more precise value of pi.
Using a function to calculate volume
python
def cylinder_volume(r, h):
    pi = 3.1416
    return pi * r ** 2 * h
radius = float(input('Radius: '))
height = float(input('Height: '))
print(f'Volume of cylinder: {cylinder_volume(radius, height)}')
Encapsulates the calculation in a function for reuse and clarity.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

All approaches run in constant time; using math.pi is slightly more accurate but has negligible performance difference.

ApproachTimeSpaceBest For
Fixed pi valueO(1)O(1)Simple, quick calculations
math.pi constantO(1)O(1)More accurate results
Function encapsulationO(1)O(1)Code reuse and clarity
💡
Use the math module's pi constant for more accurate calculations instead of a fixed value.
⚠️
Forgetting to square the radius or mixing up radius and height values.